1

I have written a JAVA program that takes input from the user and check if the user has entered the correct thing or not. I have taken the input from the Scanner class. If the user enters an invalid character like a String, I want to display 'Invalid Input'.

public class Main {
    public static void main(String[] args) {
        Scanner takeInteger = new Scanner(System.in);
        System.out.println("Enter a number");
        int enteredNumber = takeInteger.nextInt();
    }
}
2
  • And what is a valid input? what did you try until now? Commented Jul 7, 2021 at 15:49
  • A valid input is an integer. Commented Jul 7, 2021 at 16:00

5 Answers 5

4

Just ask the Scanner whether the next input is a valid int value, e.g.

Scanner takeInteger = new Scanner(System.in);
System.out.println("Enter a number");
while(!takeInteger.hasNextInt()) {
    System.out.println("Invalid Input: " + takeInteger.next());
}
int enteredNumber = takeInteger.nextInt();

This will retry the operation until the user entered a number. If you just want a single attempt, use something like

Scanner takeInteger = new Scanner(System.in);
System.out.println("Enter a number");
if(!takeInteger.hasNextInt()) {
    System.out.println("Invalid Input: " + takeInteger.next());
}
else {
    int enteredNumber = takeInteger.nextInt();
    // ... proceed with the input
}
Sign up to request clarification or add additional context in comments.

Comments

1

You will get an Exception that is InputMismatchException when an invalid input is passed.(i.e except integer value),you can use a try-catch block to hold the exception and inform the user about the invalid input. Try block , Catch block

    import java.util.*; 

    Scanner takeInteger = new Scanner(System.in);
    System.out.println("Enter a number");
    try{
       int enteredNumber = takeInteger.nextInt();
    }
    catch(InputMismatchException e) {
        System.out.println("Enter a valid input");
    }

2 Comments

It says that Cannot resolve symbol 'InputMismatchException'
@Hardik I thought you may have imported the package java.util using the wildcard import java.util.* but it looks like that you have imported only the Scanner class you can use this class -> import java.util.InputMismatchException;
1

You can use Exception handling for the same.

public class Main {
     public static void main(String[] args) {
         Scanner takeInteger = new Scanner(System.in);
         System.out.println("Enter a number");
         try {
             int enteredNumber = takeInteger.nextInt();
         }
         catch (Exception e) {
             System.out.println("Invalid Input");
         }
     }
}

2 Comments

When I implement the following code, it works. But let's say that I want to print out the addition of the numbers. When I take an input ( the second number ) and print it using System.out.println, for 'enteredNumber ,it says that 'Cannot resolve symbol 'enteredNumber''
@Hardik It seems you are taking another input or performing addition outside 'try' block . Try doing this inside the ' try ' block only after you have taken the first input .
0

You can add a try-catch block in your program to check if the user's input is a number or not.

        Scanner takeInteger = new Scanner(System.in);
        System.out.println("Enter a number");
        String input = takeInteger.next();
        int enteredNumber;
        try
        {
            enteredNumber = Integer.parseInt(input); // Input is a number
        }
        catch(NumberFormatException ex)
        {
            System.out.println("Wrong Input!"); // Invalid input
        }

Comments

0

You need to call the .nextLine method of the Scanner class and then parse to the desired type.

Example:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        String line = sc.nextLine();

        try {
            int enteredNumber = Integer.parseInt(line);
            System.out.println("You have entered: " + enteredNumber);
        } catch (Exception e) {
            System.out.println("Invalid Input");
        }
    }
}

Result with a number:

Enter a number
12
You have entered: 12

Result a text:

Enter a number
abcd
Invalid Input

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.