check if string has integer java code example

Example 1: java test if a string is a int

public static boolean isInt(String str) {
	
  	try {
      	@SuppressWarnings("unused")
    	int x = Integer.parseInt(str);
      	return true; //String is an Integer
	} catch (NumberFormatException e) {
    	return false; //String is not an Integer
	}
  	
}

Example 2: has integer java

// To Check The Given Number is Integer Or Not In Java
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter The Number");
		System.out.println(sc.hasNextInt());
	}
}