David
David

Reputation: 53

How to loop ask if a number is an integer?

x = input.nextInt();

while (!input.hasNextInt(x)) {
    System.out.println("Enter a valid int");
    x = input.nextInt();
}

while (x <=0 || x > 3) {
    System.out.println("Choose a correct gear number: ");
    x = input.nextInt();
}

switch(x) {
    case 1:
        System.out.println("You're in Gear 1");
        break;
    case 2:
        System.out.println("Gear 2");
        break;
    case 3:
        System.out.println("Gear3");
} 

I cannot figure out how to continuously loop asking for an int while x is not an int. I have tried so many things. Everytime I enter a letter or a letter number combination like Q or 23k or 2k etc. I get a mismatch error. I want to be able to check if the user is inputting something he should not in my methods.

Upvotes: 1

Views: 871

Answers (5)

abdulrafique
abdulrafique

Reputation: 304

Working Solution:

import java.util.Scanner;

public class AskGear {

public static void main(String[] args) {
    int choose = 0;
    Scanner sc = new Scanner(System.in);

    for (;;) {
        System.out.println("Enter your choice: ");
        if (!sc.hasNextInt()) {
            System.out.println("only integers!: ");
            sc.next(); // discard
            continue;
        }

        choose = sc.nextInt();

        if (choose <= 0 || choose > 3) {
            System.out.println("Choose a correct gear number");
            continue;
        }

        boolean choiceMade = false;
        switch (choose) {
        case 1:
            System.out.println("You're in Gear 1");
            choiceMade = true;
            break;
        case 2:
            System.out.println("Gear 2");
            choiceMade = true;
            break;
        case 3:
            System.out.println("Gear3");
            choiceMade = true;

        }
        if (choiceMade) {
            break;
        }
    }

}

}

Note: If you want it to run endlessly even if user enters correct gear , you can comment this code:

if (choiceMade) {
        break;
    }

Upvotes: 0

vikash vik
vikash vik

Reputation: 686

    String str = "1 2 3 4 str";
    Scanner input = new Scanner(str);
    while(input.hasNext()){
        if(input.hasNextInt()){
            int x = input.nextInt();
            switch(x) {
                case 1:
                    System.out.println("You're in Gear 1");
                    break;
                case 2:
                    System.out.println("Gear 2");
                    break;
                case 3:
                    System.out.println("Gear3");
                    break;
                default : System.out.println("Number out of range");
            } 
        }
        else {
            System.out.println("Input not a integer::" + input.next());
        }
    }

Upvotes: 0

SatyaTNV
SatyaTNV

Reputation: 4135

I want to be able to check if the user is inputting something he should not in my methods.

   int x = 0;
   while(x <=0 || x > 3){
   try {
         System.out.println("Choose a correct gear number: ");

         x = input.nextInt();
        } catch (InputMismatchException ex) {
        System.out.println("Invalid input");
        input.nextLine();//the scanner can freely read the next
        }
    } 

Accept input values 1, 2, and 3 only. If you enter any thing other than a number like 24k, then this code will throws an exception java.util.InputMismatchException and again it going to asks the input.

Upvotes: 0

Amit Bhati
Amit Bhati

Reputation: 5649

Logic is simple if you use the nextInt() method of Scanner class to read the input like 23k which is a string value, it will throw an inputmismatch exception, as it is expecting a Integer value but you are entering a String value.

So you need to read the input as a String by using next() method and then validate it for a valid integer value or not.

public static void main(String[] args){
        System.out.println("Entered inside the program....");
        Scanner console=new Scanner(System.in);
        System.out.println("Enter an input...");
        String s=console.next();
        if(null==s || !isInt(s)){
            System.out.println("Entered input is not a valid integer:"+s);
        }
        System.out.println("Entered input is a valid integer");
    }

    private static boolean isInt(String arg){
        try{
            Integer.parseInt(arg);
            return true;
        }catch(NumberFormatException nfe){
            return false;
        }
    }

Upvotes: 1

mykpc
mykpc

Reputation: 37

while(input.hasNext()) {

 if(input.hasNextInt()) {
    x = input.nextInt();
    if(x <= 0 || x > 3) {
     // throw exception or whatever your requirements is.
    }

    switch(x){
        case 1:
        System.out.println("You're in Gear 1");
            break;
        case 2:
        System.out.println("Gear 2");
            break;
        case 3:
        System.out.println("Gear3");
    } 
 }




}

Upvotes: 0

Related Questions