0

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
        }
user
  • 6,897
  • 8
  • 43
  • 79
  • 5
    Can you show us the entire method? – Mike G Nov 09 '15 at 21:21
  • Try printing the value of `oneCoordinate` before you start working with it (or using the debugger to inspect it when the exception gets thrown). What does it contain at the point of the fault? – user Nov 09 '15 at 21:26
  • @MichaelKjörling i've already tried that, it does work until the last method. The one that is shown above. – DutchStudent Nov 09 '15 at 21:29
  • If you have tried that, there is at least no evidence of it in your question. I didn't suggest you print the variables after you try reading the integers; I suggested that you print the value that you try to read the integers from. I take it from your username that you are learning, and might be in school; believe me, learning to look at bugs with the cold, hard eye of the compiler and computer is going to be *the* most useful skill you can learn in real life. Take this opportunity to practice that. Look at what *exactly* the computer is executing and what the inputs are at the point of fault. – user Nov 09 '15 at 21:31

0 Answers0