扫描仪Java问题



我正试图编写一个程序来解析我所拥有的文本文件。如果它以"#"开头,我跳到下一行。如果我扫描一个低位数字,我会将宽度设置为该数字。如果我遇到一个单词,我会将颜色设置为该单词。如果我遇到一个更大的数字,我会将x设置为第一个数字,将y设置为紧随其后的数字。利用所有这些信息,它会绘制线条。

这是我的问题。如果我在条件中扫描.next(),即使它不符合条件,它也会移动到文本文件中的下一个单词/数字,我知道它应该是这样工作的。scan.nextInt()也是如此。我不知道如何在不推进文本的情况下检查文本文档中的下一个字符串/int。

如果你理解我的问题,你能试着帮我吗?非常感谢。

package lab7;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.awt.Point;
import plotter.PolylinePlotter;
public class CheckpointTwo  {
    public static void main(String[] args) throws FileNotFoundException
    {
    File file = new File("C:/Users/Nick/workspace/project7/test.txt");
    Scanner scanner = new Scanner(file);
    PolylinePlotter plotter = new PolylinePlotter();
    while (scanner.hasNextLine())
    {
      String line = scanner.nextLine();
        Scanner scan = new Scanner(line);
        String color = "";
        int width=0;
        while(scan.hasNext())
        {
            int x=0;
            int y=0;
            if(scan.next().equals("#"))
            {
                scan.nextLine();
                continue;
            }
            else if(scan.hasNextInt()==true && scan.nextInt()<10)
            {
                width=scan.nextInt();
                color=scan.next();
            }
            else if(scan.hasNextInt()==false && scan.hasNext()==true)
            {
                color=scan.next();
            }
            else if(scan.hasNextInt()==true && scan.nextInt()>10)
            {
                x=scan.nextInt();
                y=scan.nextInt();
            }
                 plotter.startLine(color, new Point(x,y));
        }
    }
}
}

问题是在if语句中使用了scan.nextInt()scan.next()等。即使在if语句中,这仍然会使一行前进,因为仍然需要调用该方法才能获得返回值。您应该做的是获取下一行,并将其存储在if语句之前的一个变量中,然后您可以与该变量而不是方法进行比较。例如:

while (scanner.hasNextLine())
{
    String line = scanner.nextLine();
    Scanner scan = new Scanner(line);
    String color = "";
    int width=0;
    while(scan.hasNext())
    {
        int x=0;
        int y=0;
        boolean isInt = scan.hasNextInt();
        String next = scan.next();
        if(next.equals("#"))
        {
            scan.nextLine();
            continue;
        }
        else if(isInt && Integer.parseInt(next) < 10)
        {
            width=scan.nextInt();
            color=scan.next();
        }
        else if(!isInt)
        {
            color=next;
        }
        else if(isInt && Integer.parseInt(next) > 10)
        {
            x=scan.nextInt();
            y=scan.nextInt();
        }
        plotter.startLine(color, new Point(x,y));
    }
}

首先我定义了两个变量:

boolean isInt = scan.hasNextInt();
String next = scan.next();

第一变量isInt存储第二变量next是否为intnext清楚地存储scan.next()。现在,我们可以在if语句中使用这两个变量,并且不必担心意外调用scan.next()和推进Scanner。在要检查next> 10还是< 10的语句中,使用Integer.parseInt(String s)String变量next转换为int

如果这让你感到困惑,或者它没有按预期工作,请告诉我,我可以提供帮助。我希望这对你有用!

您的测试条件必须是

if(s.equals("#")) 

因为您已经读取了一行并将其存储在变量String s

  1. 确保在while循环的每次迭代中,只调用一个nextXXX方法
  2. 编写System.out.println并打印内容
  3. 在IDE中逐步执行代码(Eclipse)
  4. 如果您不使用IDE,请开始使用IDE