【问题标题】:Program Won't Read all of the data from the File程序不会从文件中读取所有数据
【发布时间】:2014-10-13 06:09:57
【问题描述】:

所以我的程序的要点是它会从文件中读取一些命令,然后根据命令执行某些操作。我的问题是我的代码没有读取 txt 文档的所有行。具体来说,它读取我测试的前四行,方法是之前在 if 语句中输入 print 语句来测试是否 input = "P"。任何帮助将不胜感激。

这里也是txt文档

P e1 10 5

P e2 19 5

P e3 11 5

P e4 18 5

R mouth 10 10 10 2

S nose 14 7 2

P p3 9 9

P p4 20 9

D

Y bad command

M p3 9 12

M p4 20 12

D



public static void main(String[] args) {

          String filename = args[0];
          File file = new File(filename);

          Scanner input=null;

          try {

               input=new Scanner(file);  

          }
          catch(java.io.FileNotFoundException ex) {
               System.out.println("ERROR: Couldn't open file: " + file);
               System.exit(1);
          }

          AsciiDisplay asciiDisplay= new AsciiDisplay();

          while(input.hasNext()) {

              if(input.next().equals("P")) {

                    String id=input.next();
                    int x=input.nextInt();
                    int y=input.nextInt();
                    Coordinate coordinate=new Coordinate(x,y);
                    Point point=new Point(id,coordinate);
                    asciiDisplay.addShape(point);

               }

              else if(input.next().equals("R")) {
                    String id=input.next();
                    int x=input.nextInt();
                    int y=input.nextInt();
                    int length=input.nextInt();
                    int height=input.nextInt();
                    Coordinate coordinate= new Coordinate(x,y);
                    Rectangle rectangle= new Rectangle(id,coordinate,length,height);
                    asciiDisplay.addShape(rectangle);

               }

               else if(input.next().equals("S")) {

                    String id=input.next();
                    int x=input.nextInt();
                    int y=input.nextInt();
                    int size=input.nextInt();
                    Coordinate coordinate= new Coordinate(x,y); 
                    Square square = new Square(id,coordinate,size);
                    asciiDisplay.addShape(square);

               }

               else if(input.next().equals("M")) {

                    String id=input.next();
                    int x=input.nextInt();
                    int y=input.nextInt();
                    Coordinate coordinate= new Coordinate(x,y);
                    asciiDisplay.moveShape(id,coordinate);

               }

               else if(input.next().equals("E")) {

                    asciiDisplay.deleteAll();

               }

               else if(input.next().equals("D")) {
                    asciiDisplay.updateGrid();
                    asciiDisplay.printGrid();

               }
               else if(input.next().equals("X")) {
                  System.out.println("Invalid command: X");


               }


          }
     }
}

【问题讨论】:

  • 那么你的调试器告诉你什么?没有调试器?你添加的用于查看程序流程和变量内容的打印语句怎么样?
  • @JarrodRoberson 可能是骗子,但不是那个。
  • 不是指定问题的副本。有一个完全不同的答案 - 我正要发布。 OP 不会尝试将整个内容读入字符串;他们正试图一点一点地处理它。

标签: java file class loops if-statement


【解决方案1】:

您的问题是您在 if 语句中反复调用 input.next(),这会不断从输入中提取更多标记。相反,您需要调用它一次并将值分配给一个变量,然后检查该变量:

String nextCommand = input.next();

或者,您可以使用开关:

switch(input.next()) {
case "P":
    // do stuff
    break;
case "R":
    // do other stuff
    break;
}

【讨论】:

    【解决方案2】:

    你在这里打电话给input.next() 太频繁了:

    if (input.next().equals("P")) {
        ...
    }
    else if(input.next().equals("R")) {
        ...
    }
    else if(input.next().equals("S")) {
        ...
    }
    ...
    

    在检查S 时,它已读取三个令牌。你需要这样的东西:

    String token = input.next();
    if (token.equals("P")) {
        ...
    } else if (token.equals("R")) {
        ...
    } else if (token.equals("S")) {
        ...
    } ...
    

    这样,您读取令牌一次,然后找到正确的代码来处理它。

    请注意,从 Java 7 开始,您还可以使用 switch 语句执行此操作:

    switch (input.next()) {
        case "P": {
            ...
            break;
        }
        case "R": {
            ...
            break;
        }
        case "S": {
            ...
            break;
        }
    }
    

    (你没有在这里有大括号,但考虑到你的块 是非平凡的,这样做是有用的,以便引入一个 每个块的新范围。您可能需要考虑拆分代码 也可以分成不同的方法...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多