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");
}
}
}
}
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...)
See more on this question at Stackoverflow