Answers for "how to check if entered number is an int in java"

1

how to check if an input is an integer java

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

public class Number {
	
	public static void main(String[] args) {
		
		int yourNumber;
		
		while(true) {	
			
			// to read inputs from user
			Scanner scan = new Scanner(System.in);
			
			try {
				System.out.print("ayy bruh enter you age : ");
				
				// throws InputMismatchException if it's not a number
				yourNumber = scan.nextInt(); 
				
				scan.close();
				break;
			}catch(InputMismatchException notNumber) { // << catch the exception and print the error message
				System.err.println("huh I can understand numbers,"
						+ "ENTER A NUMBER !");
			}
		}
		
		// print the result :))
		System.out.printf("so your age is %d, ..AWESOME !! :))", yourNumber);
		
	}
	
}
Posted by: Guest on June-27-2021
2

how to check if number is integer in java

if(x == (int)x){
 println("x is an integer");
}
Posted by: Guest on October-28-2020

Code answers related to "how to check if entered number is an int in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language