Check Input Is Integer or Character in Java

Learn how to check input is integer or character in Java using loops and conditional statements. The program accepts input from the user and determines whether it contains only digits, only alphabetic characters, or other special characters.

Code Explanation (Step-by-Step)

  • Creates a Scanner object named sc. System.in is used to read input from the keyboard.
  • sc.next() reads the user input and stores it as a String.
  • Calls the checkData() method and passes the user input str as an argument.
  • The for loop iterates through each character of the input string .
  • str.charAt(i) retrieves the current character from the string.
  • if (!((ch >= ‘A’ && ch <= ‘Z’) || (ch >= ‘a’ && ch <= ‘z’))) checks whether the character is an uppercase letter (A-Z) or a lowercase letter (a-z).
  • if (!(ch >= ‘0’ && ch <= ‘9’)) checks whether the character is a digit (0-9).
  • if (isAlphabet) checks whether all characters in the input are alphabets.
  • else if (isDigit) checks whether all characters in the input are digits.
  • If all characters are alphabets, the program prints that the input is a character/string.
  • If all characters are digits, the program prints that the input is an integer.
  • Otherwise, the program prints that the input is neither an integer nor a character.
				
					import java.util.Scanner;

public class CheckIntChar {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter input value : ");
        String str = sc.next();

        System.out.println(checkData(str));
    }

    public static String checkData(String str) {
        boolean isAlphabet = true;
        boolean isDigit = true;

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) {
                isAlphabet = false;
            }

            if (!(ch >= '0' && ch <= '9')) {
                isDigit = false;
            }
        }

        if (isAlphabet) {
            return "It is a character/string and value is: " + str;
        } else if (isDigit) {
            return "It is an integer and value is: " + str;
        } else {
            return "This is neither an integer nor a character";
        }
    }
}
				
			

Output :
1- Enter input value :
Hello

It is a character/string and value is: Hello

2- Enter input value :
12345

It is an integer and value is: 12345

3- Enter input value :
@#

This is neither an integer nor a character

FAQ
How do I check if an input is an integer in Java?
You can check whether all characters in the input are digits using a loop or the Character.isDigit() method.
How do I check if an input contains only letters in Java?
Use Character.isLetter() or compare each character against the ranges A-Z and a-z.
What happens if input contains special characters?
The program identifies it as neither an integer nor a character.
Can this program validate mixed input?
Yes. Inputs such as abc123 or @# will be classified as neither an integer nor a character.