check if a number is an integer
whats best way to check if a number is an integer?
Ive heard of doing it this way
Code :
test = Math.floor(num1);
if(test == num1)
System.out.println("integer");
any other ways? (and is there any time where i could run into a problem with this method?)
Re: check if a number is an integer
Hello rsala004.
It could also be done like this:
Code :
import java.util.Scanner;
public class Konnor {
public static void main(String[] args){
System.out.println("Enter Integer Value: ");
Scanner sc = new Scanner(System.in);
try {
int myInt = Integer.parseInt(sc.next());
System.out.println("Integer value is: " + myInt);
}
catch(NumberFormatException e) {
System.out.println("You have not entered an Integer!");
}
}
}
Re: check if a number is an integer
Where a is any primitive data type byte, short, int, long, float, double, or char. If you want to test larger numbers, use long.
Re: check if a number is an integer