user input check int only

牧云@^-^@ 提交于 2019-12-02 01:36:07

问题


I am trying to have my user input not crash my program by restricting what the user can input such as:

  1. only being an int
  2. being between 1-30

The code that I've written works only up to a certain point. If you enter something thats not an int it will check it and ask you to enter again. Then again if you keep typing anything but an int. I have another while loop if it does type an int, and if it's outside the 1-30 zone then it will ask the user to input again. However after that if the user types another "anything but an int" the program will crash. I've tried to combine both the sc.hasnextint() and the check for input between 1-30 condition but if i put the sc.nextint() before the sc.hasnextint() and the user enters anything but an int, the program crashes. If I put it after the condtion loop, then the userinput will not be declared.

int choose;
System.out.print("type an integer: ");
Scanner sc=new Scanner(System.in);

while (!sc.hasNextInt() ) { 
    System.out.println("only integers!: "); 
    sc.next(); // discard 
} 

choose=sc.nextInt();

while (choose<=0 || choose>30)
{
    System.out.print("no, 1-30: ");
    choose=sc.nextInt();
}
sc.close();

回答1:


You need to combine the two loops, so that both checks happen every time the end-user enters something new:

for(;;) {
    if(!sc.hasNextInt() ) { 
        System.out.println("only integers!: "); 
        sc.next(); // discard
        continue;
    } 
    choose=sc.nextInt();
    if( choose<=0 || choose>30)
    {
        System.out.print("no, 1-30: ");
        continue;
    }
    break;
}

After the loop exits, choose is a number between 1 and 30, inclusive.




回答2:


do:
get number from user
if non integer format is entered{
number = -1;}
while: 1 < number < 30



回答3:


    String choose = "";
    System.out.println("Test if input is an integer. Type 'quit' to exit.");
    System.out.print("Type an integer: ");
    Scanner sc=new Scanner(System.in);

    choose = sc.nextLine();

    while (!(choose.equalsIgnoreCase("quit"))) {
        int d = 0;
        try {
            d = Integer.parseInt(choose);

            if (!(d > 0 && d < 31)) {
                System.out.println("Being between 1-30");
            } else {
                System.out.println("Input is an integer.");
            }
        } catch (NumberFormatException nfe) {
            System.out.println("Enter only int");
        }

        System.out.print("Type an integer to test again or 'quit' to exit: ");
        sc = new Scanner(System.in);
        choose = sc.nextLine();
    }

    sc.close();
    System.out.print("Program ends.");



回答4:


See dasblinkenlight's awnser with the NumberFormatException catch. I was thinking of doing that. This also works too:

You need to combine the two loops like so:

while(true) {
if(!sc.hasNextInt) {
System.out.println("Only Integers!");
continue;
}
choose = sc.nextInt();
if(choose <= 0) {
System.out.println("The number you entered was too small.");
continue;
} else if(choose > 30) {
System.out.println("The number you entered was too large.\nMax: 30");
continue;
}
break;
}
sc.close();


来源:https://stackoverflow.com/questions/12487734/user-input-check-int-only

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