Tanner Cray
Tanner Cray

Reputation: 7

How to manage incorrect input

I am trying to take input in the form of two different integers separated by a space ex: 14 2.

I am having trouble finding a way to prompt the user to try again if the input is either a double instead of an int, a character, or basically anything that isn't two integers separated by a space.

I dont really want to use exceptions and try/catch statements because I don't understand them.

I would rather either take in a string[] arr and use split(" ") or just do console.nextInt();. Any feedback would be great. I know it is not pretty.

Scanner console = new Scanner(System.in);
boolean ok = true;
boolean ok2 = true;
while (ok = true){
for(int i = 0; i<=2; i++){

if(!console.hasNextInt()){
    kb.next();
    System.out.println("Bad input, try again");
    ok2 false;
}
else if(i = 0 && ok2 = true){
    int a = console.hasNextInt();
}
else if(i = 1 && ok2 = true){
    int b = console.hasNextInt();
    ok = false; //means there are two #s and the program can continue with a and b   

}

else{
}
 } 

Upvotes: 0

Views: 352

Answers (3)

gprathour
gprathour

Reputation: 15333

I am trying to write as simple and as easy code as possible,

Scanner console = new Scanner(System.in);
System.out.print("Enter two numbers  ");

String str = console.readLine();

String values[] = str.split(" ");

int n1, n2;

try{
    n1 = Integer.parseInt(values[0]);
    //Convert first String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("First number is not integer");
}

try{
    n2 = Integer.parseInt(values[1]);
    //Convert second String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("Second number is not integer");
}

Note

This code is based on assumption that there will be only Two elements entered by the user. NOT more than two. In that case, code will be needed to change.

Upvotes: 1

fipple
fipple

Reputation: 360

Another solution using Scanner#hasNextInt() to validate the input:

    final Scanner console = new Scanner(System.in);
    final List<Integer> input = new ArrayList<Integer>();

    while (true)
    {
        System.out.print("Please enter an integer : ");

        if (console.hasNextInt())
        {
            input.add(console.nextInt());
        }
        else
        {
            System.out.println("Bad input, try again ...");
            console.next();
        }
        if (input.size() >= 2)
        {
            break;
        }
    }

    System.out.println("\n");
    System.out.println("First integer entered was : " + input.get(0));
    System.out.println("Second integer entered was : " + input.get(1));

Upvotes: 1

Ninad Pingale
Ninad Pingale

Reputation: 7069

Following code accepts input till it gets positive number only. Negative & zero numbers are handled by while loop condition. and characters are handled using !sc.hasNextInt() in another while loop condition.

       Scanner sc = new Scanner(System.in);
        int number;
        do {
            System.out.println("Please enter a positive number : ");
            while (!sc.hasNextInt()) {
                System.out.println("That is not a valid number.");
                sc.next(); 
            }
            number = sc.nextInt();
        } while (number <= 0);
        System.out.println("Recieved a Positive number = " + number+". Thanks!");

Output-

Please enter a positive number : 
-1
Please enter a positive number : 
0
Please enter a positive number : 
n
That is not a valid number.
6
Recieved a Positive number = 6. Thanks!

Upvotes: 1

Related Questions