Answers for "how to check if entered value is integer 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
0

java check if int

//"% (modulo)" calulates the remainder when the value on the left is divided by the value on the right

float number = 20;

if(number%1 == 0){
	println("It is an integer");
} else {
	println("It is not an integer");
}
Posted by: Guest on June-15-2020

Code answers related to "how to check if entered value is integer in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language