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.

Results 1 to 4 of 4

Thread: check if a number is an integer

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default 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
            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.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: check if a number is an integer

    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 reputation by clicking the button on their useful posts.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: check if a number is an integer

    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.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: check if a number is an integer

        Character.isDigit()

    // Json

Similar Threads

  1. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  2. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  3. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM
  4. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM