0

So the gist of my program is that it will read some commands from a file and then perform certain operations judging by the commands. My problem is that my code isn't reading all of the lines of the txt document. Specifically, it reads the first four lines which I tested by previously inputting a print statement inside of the if statement that tests to see if the input = "P". Any help would be much appreciated.

Here is the txt document as well

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");


               }


          }
     }
}
  • 1
    So what is your debugger telling you? no debugger? how about the print statements you add to view program flow and variable contents? – John3136 Oct 13 '14 at 06:13
  • @JarrodRoberson Probably a dupe, but not of that one. – chrylis -cautiouslyoptimistic- Oct 13 '14 at 06:13
  • 2
    This is *not* a duplicate of the specified question. There's a completely different answer - which I was about to post. The OP isn't trying to read the whole content into a string; they're trying to process it bit by bit. – Jon Skeet Oct 13 '14 at 06:14

2 Answers2

2

Your problem is that you repeatedly call input.next() in your if statements, which keeps pulling more tokens off the input. Instead, you need to call it once and assign the value to a variable, then check that variable:

String nextCommand = input.next();

Alternately, you could use a switch:

switch(input.next()) {
case "P":
    // do stuff
    break;
case "R":
    // do other stuff
    break;
}
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

You're calling input.next() far too often here:

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

By the time it's checked for S, it's read three tokens. You need something like:

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

This way, you read the token once, then find the right piece of code to handle it.

Note that as of Java 7, you could also do this with a switch statement:

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

(You don't have to have braces here, but given that your blocks are non-trivial, it would be useful to do so in order to introduce a new scope per block. You might want to consider splitting the code out into separate methods, too...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194