0

As title states, have a bit of an issue reading things correctly. The catch here is that I am reading only a bit of the file outside of a while loop, and the rest inside. I think that works fine because a piece of the file is read into every conditional.

Here is my code:

(rest of the code isn't pasted in for conciseness)

Scanner infp = new Scanner(new File("customerdata.txt"));

    int timeEntered = infp.nextInt();

    System.out.println(timeEntered);

    while (infp.hasNext()) {
        if (timeEntered == clock)
        {
            String type = infp.next();
            int itemNum = infp.nextInt();

            if (type.equals("REFUND"))
            {
                itemPrice = infp.nextDouble();

            }
        }
    }

My InputMismatchException error occurs at int itemNum = infp.nextInt();.

Notice, by now I have successfully read the String type. At first my idea rested in a carriage return but then I remembered that is only the case for user input.

Here is an example of the file being read:

0 R 324 32.53

Any and all help is appreciated.

Kraw24
  • 143
  • 1
  • 7
  • I cannot fully debug this code, because the definition of `clock` is missing, but it might be related to `type.equals("REFUND")` evaluating to false, which avoids invoking `nextDouble`. At least with the example given its seems to be not intended. – Izruo Nov 09 '17 at 22:56
  • Clock is an integer initialized to zero. I see what you're saying but the error occurs BEFORE that if statement even comes to light. It occurs immediately above. – Kraw24 Nov 09 '17 at 22:58
  • It occurs on the second 'walk-through', because `32.53` is not consumed and therefore `infp.hasNext()` evaluates to `true`. – Izruo Nov 09 '17 at 23:05
  • It's happening on the second round. Try debugging and you'll see. – shmosel Nov 09 '17 at 23:05
  • I incremented the clock outside of the while loop so that the conditional only occurs once. Fixed the problem, everything reads fine now. Thanks for the responses! – Kraw24 Nov 09 '17 at 23:16

0 Answers0