Check if Input is Integer in Java

In this tutorial, we will learn how to determine whether the given input is an integer is or not. If the entire input contains only digits i.e. 0-9 then it will be considered as an integer. To implement a program of checking valid integer we will use three methods:

  • Checking valid integer using Integer.parseInt() method
  • Checking valid integer using Scanner.hasNextInt() method
  • Checking valid integer using Character.isDigit() method

1)Example of checking valid integer using Integer.parseInt()

In this example, we will try to parse a string to integer if that string contains only digits then it will not throw an exception and we can print output that the given input is a valid integer. In a case given input is not a valid integer then it will throw an exception, at that time we can handle an exception by giving a message that the current input is not a valid integer.

public class StudyTonight 
{ 
	public static void main(String[] args)   
	{ 
		String input = "1234";           
		try 
		{ 
			Integer.parseInt(input); 
			System.out.println(input + " is a valid integer"); 
		}  
		catch (NumberFormatException e)  
		{ 
			System.out.println(input + " is not a valid integer"); 
		} 
	} 
} 


1234 is a valid integer

On the other hand, if we provide input other than integer it will give output as the given string is not a valid integer.

public class StudyTonight 
{ 
	public static void main(String[] args)   
	{ 
		String input = "ab1234c";           
		try 
		{ 
			Integer.parseInt(input); 
			System.out.println(input + " is a valid integer"); 
		}  
		catch (NumberFormatException e)  
		{ 
			System.out.println(input + " is not a valid integer"); 
		} 
	} 
} 


ab1234c is not a valid integer

2) Example of checking valid integer using Scanner.hasNextInt() method

The Scanner.hasNextInt() method checks whether the current input contains an integer or not. If the integer occurred in input this method will return true otherwise it will return false.

import java.util.Scanner;
public class StudyTonight 
{ 
	public static void main(String[] args)   
	{ 
		Scanner sc = new Scanner(System.in);   
		if(sc.hasNextInt()) {
		   System.out.println(sc.nextInt()+" is valid Integer");
		}
		else
		{
			 System.out.println(sc.nextInt()+" is valid Integer");
		}
		sc.close();
	} 
}


123
123 is valid Integer

3) Example of checking valid integer using Character.isDigit() method

The Character.isDigit() is a method that checks whether the current character is a digit(number) or any other character. The logic behind this code is, we are given a string and we will check each character and any character which is not a digit then we set a flag to false, the flag must be set true initially. After traversing the string we check the status of the flag if it is true then we will print the current input as a valid integer.

public class StudyTonight 
{ 
	public static void main(String[] args)   
	{ 
		String input = "1234";           
		Boolean flag=true;
		for(int a=0;a<input.length();a++)
		{
			if(a==0 && input.charAt(a) == '-')
				continue;
			if( !Character.isDigit(input.charAt(a)))
				flag=false;          	   
		}
		if(flag)
		{
			System.out.println(input+" is valid Integer");
		}
	} 
} 


1234 is valid Integer

Conclusion:

In this article, we learned to check given input is a valid integer or not. Primarily we can use Integer.parseInt() method, Scanner.hasNextInt() method and Character.isDigit() method all the methods are equally efficient. Scanner.hasNextInt() can be used only in a case we are accepting input using Scanner class.