I'm quite new in programming and for an assignment I need to scan in Coordinates. The input is given as follows:
5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4
And I am currently trying to get my scanner working. I've used two while loops and three delimiters, which are "=" , " " and ",". This is what I've got in my last loop:
Scanner XYScanner = new Scanner(oneCoordinate);
XYScanner.useDelimiter(",");
int testX = XYScanner.nextInt();
int testY = XYScanner.nextInt();
out.printf("%d,%d\n",testX,testY);
And it will print out the following:
5,4
4,5
8,7
6,3
3,2
9,6
4,3
7,6
9,8
5,5
7,8
6,5
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Pirate.Pirate.readXAndYValues(Pirate.java:28)
at Pirate.Pirate.readRowOfCoordinate(Pirate.java:38)
at Pirate.Pirate.readAllCoordinates(Pirate.java:57)
at Pirate.Pirate.start(Pirate.java:64)
at Pirate.Pirate.main(Pirate.java:68)
As you can see, it does read all the coordinates except the last one. Does someone know what I got to do and can tell me why?
Update 09-11-2015 22:36 :
Reply@ Its more than one method, but if i put it in one method it will look like this
input.useDelimiter("=");
while(input.hasNext()){
String oneRowOfCoordinates = input.next();
readRowOfCoordinate(oneRowOfCoordinates);
}
Scanner scanOneCoordinate = new Scanner(oneRowOfCoordinates);
scanOneCoordinate.useDelimiter(" ");
while(scanOneCoordinate.hasNext()){
String oneCoordinate = scanOneCoordinate.next();
readXAndYValues(oneCoordinate); // this is the method that is shown above
}