简体   繁体   中英

How can i make incorporate a 'int only' input in this loop?

I've been messing around with this code for about an hour now trying to make it loop if the user inputs a character instead of a number

    System.out.println("Enter level student last completed (0-3): "); 
    int level = in.nextInt();
    while (level > 3 || level < 0){
        System.out.println("Please enter a valid level!: ");
        level = in.nextInt();
    }

I thought of adding !in.hasNextInt() to the line while (level > 3 || level < 0) to make it

while (!in.hasNextInt() || level > 3 || level < 0)

but this doesn't help as the programme still crashes if a character is input.

edit:

    System.out.println("Enter level student last completed (0-3): "); 
    int level = 1; //in.nextInt();
    while (in.hasNextInt()==false || level > 3 || level < 0){
        in.next();
        System.out.println("Please enter a valid level!: ");
    }
    level = in.nextInt();

You can create a while loop to repeatedly ask for an input until it gets the kind it likes

Basic solution

    int i;
    while(scan.hasNextInt()==false){ //keep asking until it gets something it likes
        scan.next(); //<--consume bad input, important!
        System.out.println("Only integers are valid");
    }
    i=scan.nextInt();

    System.out.println(i);

Advanced solution

You can package this up into method which will make life easier when we want to incorporate more logic

public static int getSafeInteger(){
    Scanner scan=new Scanner(System.in); //if using scanner over and over consider passing the scanner as an argument
    while(scan.hasNextInt()==false){
        scan.next();
        System.out.println("Only integers are valid");
    }
    return scan.nextInt();
}

Then we can use that method within your existing loop

    System.out.println("Enter level");

    Scanner scan=new Scanner(System.in);

    int level=getSafeInteger();
    while (level > 3 || level < 0){
        System.out.println("Please enter a valid level!: ");
        level = getSafeInteger();
    }

使用nextLine()并尝试使用Integer.parseInt(String)其解析为整数。

You could do something like this:

int level;
while (true) {
    if (in.hasNextInt()) {
        // now we know at least the input is a number
        if (level > 3 || level < 0) {
            // aww, it's an invalid number :(
            in.nextLine(); // clear bad input
            System.out.println("Please enter a valid level (0-3)!: ");
        } else {
            // hooray! store the input now
            level = in.nextInt();
            break; // out of the infinite loop
        }
    } else {
        // input is not a number
        in.nextLine(); // clear bad input
        System.out.println("Please enter a number!: ");
    }
}

Try this:

Scanner sc = new Scanner(System.in);
int level;

System.out.printf("Enter level student last completed (0-3): "); 
while(true) {
    try {
        level = Integer.parseInt(sc.next());
        if (level < 4 && level > -1) {
            break;
        }
    } catch (Exception ex) { }
    System.out.printf("Please enter a valid level!: ");
}

you can set default value of level more than 3 and do following

            do
            {
                System.out.println("Enter level student last completed (0-3): ");
                try{
                 level = in.nextInt();
                }catch(InputMismatchException e){
                    System.out.println("Not a valid Number");
                    in.nextLine();
                }
            }while (level > 3 || level < 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM