-1
System.out.println("Insert first number : ");
Scanner ad = new Scanner(System.in);
int y = ad.nextInt();
System.out.println("Insert Second Number: ");
Scanner er = new Scanner(System.in);
int x = er.nextInt();

int z;
z = x + y;

if (z > 10) {
    System.out.println(z + " is greater than 10");
} else if (z < 9) {
    System.out.println(z + " is less than 10");
} else {
    System.out.println(z + " is equal to 10");
}

I want the output of each if statements to be error when I put character instead of numbers. And make an output appear that the character I have entered is invalid.

5
  • Why you use 2 Scanner instances? Commented Oct 23, 2017 at 19:42
  • You should check if the value is an int or not at the start when you're accepting the value from the user. Commented Oct 23, 2017 at 19:44
  • 1
    Possible duplicate of Java.util.scanner error handling Commented Oct 23, 2017 at 19:44
  • Also, more hints at stackoverflow.com/questions/2496239/… Commented Oct 23, 2017 at 19:46
  • I think to input the two variables, I just started learning java yesterday I just code there the if, else if, else statements but he add scanner and wanted me to analyze the whole thing. Commented Oct 23, 2017 at 19:47

1 Answer 1

0

When you use the nextInt method, it will throw an InputMismatchException if the input value is not a number, then you can use a try-catch block and a loop to validate. You can use a helper method to do this and avoid repeating yourself while reading those values:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {
        int x = readNumber();
        int y = readNumber();

        int z;
        z = x + y;

        if (z > 10) {
            System.out.println(z + " is greater than 10");
        } else if (z < 9) {
            System.out.println(z + " is less than 10");
        } else {
            System.out.println(z + " is equal to 10");
        }
    }

    private static int readNumber() {
        Scanner scanner = new Scanner(System.in);
        boolean validInput;
        int result = 0;
        do {
            try {
                System.out.print("Insert a number : ");
                result = scanner.nextInt();
                validInput = true;
            } catch (InputMismatchException e) {
                System.out.println("Not a valid number!");
                validInput = false;
                scanner.nextLine(); // to consume the endline character
            }
        } while (!validInput);
        return result;
    }
}

Another alternative would be using the hasNextInt method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.