我在编程方面很陌生,而且我需要在Coordinates中进行扫描。输入如下:
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
我正在尝试让我的扫描仪正常工作。我使用了两个while循环和三个分隔符,分别是“=”,“”和“,”。这就是我上一次循环中的内容:
Scanner XYScanner = new Scanner(oneCoordinate);
XYScanner.useDelimiter(",");
int testX = XYScanner.nextInt();
int testY = XYScanner.nextInt();
out.printf("%d,%d\n",testX,testY);
它将打印出以下内容:
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)
如您所见,它确实读取了除最后一个之外的所有坐标。有人知道我要做什么,可以告诉我为什么吗?
更新09-11-2015 22:36:
回复@它有多个方法,但如果我把它放在一个方法中,它将看起来像这样
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
}