0

Edited (3/18/2016 at 9:10 PM):

I am trying to write a program that loops until valid values are inputted by the user--month, day, and year. Note that year is a string so that, later in the program, I can print it out correctly if the year was something like "01".

while (true){
  try{
    System.out.println("Please enter a month in numeric form");
    month = input.nextInt();
    System.out.println("Please enter a day in numeric form");
    day = input.nextInt();
    System.out.println("Please enter a two-digit year");
    if (input.hasNextInt() == true){
        year = input.next();
    }
    else{
        throw new java.util.InputMismatchException();
    }
    break;
  }
  catch(Exception e){
    System.err.println(e);
    System.out.println("\nOne of your inputs was not valid.");
    System.out.println(input.nextLine()); // problem line
  }
}

The last line of the catch block seems to be causing the odd behavior. I have decided to print that line to see what input.nextLine() contains; if I leave it as input.next(), the program works perfectly. Here is the output if I enter some bad input for the year in the above code:

Please enter a month in numeric form
8
Please enter a day in numeric form
2
Please enter a two-digit year
badinput
java.util.InputMismatchException

One of your inputs was not valid.
Please enter a month in numeric form
java.util.InputMismatchException

One of your inputs was not valid.
badinput
Please enter a month in numeric form

The output I should be getting (which is what happens if I I leave it as input.next()) is

Please enter a month in numeric form
8
Please enter a day in numeric form
2
Please enter a two-digit year
badinput
java.util.InputMismatchException

One of your inputs was not valid.
badinput
Please enter a month in numeric form

It is a minute difference but I thought I understood the behavior of nextLine() and next()--now it's clear my intuition is wrong. To my understanding, next() consumes the current token while nextLine() consumes the entire current line--shouldn't they accomplish the same thing here? Can someone explain?

Quantum Dot
  • 395
  • 1
  • 5
  • 12
  • 4
    Check the link in the comment above. Everything has already been explained there :) – Yassin Hajaj Mar 18 '16 at 21:48
  • @Yassin Hajaj I've looked at the links and I'm still unsure as to why two nextLine()s are required here. I'm aware of the issue with nextXXX() methods not consuming newline characters, which was my initial logic when I put the nextLine() in the catch block. I still cannot explain the behavior in the code here. – Quantum Dot Mar 18 '16 at 22:03
  • I tested your code and am unable to find another problem than the one explained in the duplicate. Would you mind being more specific? – Yassin Hajaj Mar 18 '16 at 22:08
  • If input.hasNextInt() evaluates to false, the else block will run. Because I didn't do anything with the input other than check if it's an integer, the scanner should have something like this (to my understanding): "[bad input] _newline_". The input.nextLine() in the catch block should just skip that entire line and allow for new input to be entered on the second iteration, to my understanding. The problem is that for some reason there's still a newline character (?) leftover even after I call input.nextLine(). Where is that coming from? – Quantum Dot Mar 18 '16 at 22:18
  • @Yassin Hajaj I have looked further into the code and see that it comes down to a difference between next() and nextLine(). However, although I thought I understood the difference from searching on stackoverflow before, the behavior escapes my intuition in this case. I have updated my question to elaborate on this and show the outputs I get in terminal. – Quantum Dot Mar 19 '16 at 01:19

0 Answers0