whats best way to check if a number is an integer?
Ive heard of doing it this way
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?)
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
whats best way to check if a number is an integer?
Ive heard of doing it this way
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?)
Last edited by rsala004; August 15th, 2009 at 04:51 PM.
Hello rsala004.
It could also be done like this:
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!"); } } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputationby clicking the
button on their useful posts.
Looking for a Java job? Visit - Java Programming Careers
if (a == (int) a)
Where a is any primitive data type byte, short, int, long, float, double, or char. If you want to test larger numbers, use long.
if (a == (long) a)
Last edited by helloworld922; August 15th, 2009 at 02:38 PM.
Character.isDigit()
// Json