Answers for "how to check if an input is a number 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

Code answers related to "how to check if an input is a number in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language