checking if input from scanner is int with while loop java

流过昼夜 提交于 2019-12-01 13:32:30

问题


I basically want the following while loop to check if the input is an integer. It cannot contain decimals because it is pointing to an array. If the value entered is a decimal it should prompt the user again. Problem is I get two prompts before the while loop starts with this code. Any ideas?

      System.out.print("Enter month (valid values are from 1 to 12): ");
    Scanner monthScan = new Scanner(System.in);
    int monthInput = monthScan.nextInt();
    // If the month input is below 1 or greater than 12, prompt for another value
    while(monthInput<1 || monthInput>12 || !monthScan.hasNextInt())
    {
        System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");
        monthInput = new Scanner(System.in).nextInt();
    }

thanks

EDIT: The current output gives the following:

Enter month (valid values are from 1 to 12): 2
2

Notice how I had to enter 2 twice even though it is a valid value.


回答1:


A small modification to your program solves the problem

 System.out.print("Enter month (valid values are from 1 to 12): ");
        Scanner monthScan = new Scanner(System.in);
       int monthInput = monthScan.nextInt();
        // If the month input is below 1 or greater than 12, prompt for another value
        while((monthInput<1 || monthInput>12) )
        {
            System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");

            monthInput = monthScan.nextInt();
        }
        System.out.println("I am here");

Output:

Enter month (valid values are from 1 to 12): -5
Invalid value! Enter month (valid values are from 1 to 12): -5
Invalid value! Enter month (valid values are from 1 to 12): -2
Invalid value! Enter month (valid values are from 1 to 12): 5
I am here

Hope this helps you.




回答2:


Check that the input is integer with hasNextInt() before you call nextInt(). Otherwise nextInt() throws an InputMismatchException when user types a non integer.

int monthInput;

System.out.print("Enter month (valid values are from 1 to 12): ");
Scanner monthScan = new Scanner(System.in);

if (monthScan.hasNextInt()) {
    monthInput = monthScan.nextInt();
} else {
    monthScan.next();   // get the inputted non integer from scanner
    monthInput = 0;
}

// If the month input is below 1 or greater than 12, prompt for another value
while (monthInput < 1 || monthInput > 12) {
    System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");
    if (monthScan.hasNextInt()) {
        monthInput = monthScan.nextInt();
     } else {
       String dummy = monthScan.next();
       monthInput = 0;
    }
}



回答3:


you can check like this

System.out.print("Enter month (valid values are from 1 to 12): ");
Scanner monthScan = new Scanner(System.in);

while(monthScan.hasNext())
{
   if(!monthScan.hasNextInt() && (monthInput<1 ||  monthInput>12))
   {
       System.out.print("Invalid value! Enter month (valid values are from 1 to 12):"); 
       continue;
   }

   // If the month input is below 1 or greater than 12, prompt for another value
  int monthInput = monthScan.nextInt();
  //do your logic here   
  break;//use the break after logic 

}

update
Use break after your logic so that It will exit after valid input.



来源:https://stackoverflow.com/questions/17985575/checking-if-input-from-scanner-is-int-with-while-loop-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!