【问题标题】:How to skip a leading string when scanning text file?扫描文本文件时如何跳过前导字符串?
【发布时间】:2020-10-16 17:00:30
【问题描述】:

我正在制作一个使用文本文件中的指令绘制基本图像的程序。指令格式为:

SIZE 1000 500

// GROUND
LINE 0 350 1000 350
LINE 0 351 1000 351
LINE 0 352 1000 352
LINE 0 353 1000 353

这是我的代码:

public void start(Stage stage) {
        int fwidth = 0;
        int fheight = 0;
        try {
            Scanner obj = new Scanner(new File("scene.txt"));
            while(obj.hasNextLine()){
                String str = obj.nextLine();
                if(str.contains("SIZE")){
                    String a = "SIZE";
                    obj.skip(a);
                    System.out.println('b');
                    fwidth = obj.nextInt();
                    fheight = obj.nextInt();
                }
                if(str.contains("LINE")){
                    obj.skip("LINE");
                    System.out.println('a');
                }
            }

这是一个 NoSuchElementException。我假设这是因为 fwidth 和 fheight 将前导字符串作为整数,但我不知道如何让扫描仪在一开始就跳过字符串,一旦它知道它是什么类型的指令就读取数字它是.任何帮助表示赞赏

【问题讨论】:

  • 就个人而言,我会阅读整行然后split() 在空间上。

标签: java string java.util.scanner


【解决方案1】:

几个建议:

首先,我不认为

Scanner.skip()

做你认为它做的事。 .skip() 方法的目的是告诉扫描器在读取行时“跳过”行,而不是跳过您当前所在的行。无论如何,这将在您下次调用 .nextLine() 时完成。

我会完全删除您对 .skip() 的所有调用。此外,这更像是一种偏好,但我会使用 switch 语句而不是多个 if。它使您的代码更具可读性。

其次,正如 Johnny 在 cmets 中提到的,使用 .split() 可能会更好,因为根据我的经验 .nextInt() 会产生意想不到的结果。因此,您的代码将如下所示:

while(obj.hasNextLine()){
                String[] strArray = obj.nextLine().split(" ");
                switch(strArray[0]){
                  case "SIZE":
                    fwidth = Integer.parseInt(strArray[1]);
                    fheight = Integer.parseInt(strArray[2]);
                  break;
                 case "LINE":
                 //do nothing
                 break;
                }
            }

【讨论】:

    【解决方案2】:

    您可以更轻松地做到这一点:

    String str = obj.nextLine();
    String[] arr = str.split("\\s+");// Split on whitespace
    if("SIZE".equals(arr[0])) {
        fwidth = Integer.parseInt(arr[1]);
        fheight = Integer.parseInt(arr[2]);
    } else if("LINE".equals(arr[0])) {
        //...
    }
    

    【讨论】:

      【解决方案3】:

      正如cmets中提到的,可以使用obj.next()逐字扫描,而不是使用nextLine()

      Scanner obj = new Scanner(new File("scene.txt"));
      int fwidth, fheight;
      int num1, num2, num3, num4;
      while(obj.hasNextLine() && obj.hasNext()){
          String str = obj.next();
          if(str.contains("SIZE")){
              String a = "SIZE";
              fwidth = obj.nextInt();
              fheight = obj.nextInt();
              System.out.println("fwidth : " + fwidth + ", fheight : " + fheight);
          } else if(str.contains("LINE")){
              num1 = obj.nextInt();
              num2 = obj.nextInt();
              num3 = obj.nextInt();
              num4 = obj.nextInt();
              System.out.println("num1 : " + num1 + ", num2 : " + num2 + ", num3: " + num3 + ", num4: " + num4);
          }
      }
      

      我用您提供的文件对此进行了测试,它似乎有效:

      src : $ java ScannerLeading 
      b
      fwidth : 1000, fheight : 500
      num1 : 0, num2 : 350, num3: 1000, num4: 350
      num1 : 0, num2 : 351, num3: 1000, num4: 351
      num1 : 0, num2 : 352, num3: 1000, num4: 352
      num1 : 0, num2 : 353, num3: 1000, num4: 353
      

      【讨论】:

        猜你喜欢
        • 2011-02-17
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        • 2020-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多