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 6 of 6

Thread: How to make Java determine if a values is an integer or not.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry How to make Java determine if a values is an integer or not.

    In my code I'm trying to test if a value is an integer or not. If the entered value is an integer, than I let the user proceed; However, If it isn't the user must go back and enter a valid value. I have no idea what syntax or methods I need to use to make this work. In visual basic I know it's something like "TryParse" but I'm sure it's different in Java. Any Ideas?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make Java determine if a values is an integer or not.

    You could always try this:
    int number;
    int number2;
    Scanner console = new Scanner(System.in);

    try
    {
    number = console.nextInt();
    // OR if using JOptionPane
    number2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer.", "Enter an integer.", JOptionPane.PLAIN_MESSAGE));

    }

    catch(InputMismatchException imeRef)
    {
    String str = console.next();
    System.out.println("Invalid.");
    }

    // OR if using JOptionPane

    catch(NumberFormatException nfeRef)
    {
    JOptionPane.showMessageDialog(null, "Invalid.");
    }

    NOTE: the two int numbers are alternate examples. InputMismatch is for console and NumberFormatException is for JOptionPane.
    Don't use both numbers. Also, you may need a while loop and a boolean that will be set to true if the Exception is not caught and originally the boolean is false and therefore, as long as it's not an int, it will keep asking.

    I know for InputMismatchException, you have to clean out the invalid results, which is what I did with that String thing in the catch block.

    I can't recall if you'd need to reset it for NumberFormatException.
    Last edited by javapenguin; August 17th, 2011 at 04:50 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    MiniatureBeast (August 17th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to make Java determine if a values is an integer or not.

    You don't provide any code, so the question is a bit out of context. Do you mean you need to check if a String is a valid int? Then you can a) loop over all the chars and check if each one matches 0-9, b) Use Integer.parseInt in a try/catch - if no exception is thrown its valid, if an exception is thrown it is not valid.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to make Java determine if a values is an integer or not.

    c) Use a regular expresion
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    MiniatureBeast (August 17th, 2011)

  7. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to make Java determine if a values is an integer or not.

    If the entered value is an integer
    <autism>Integer.parseInt() and Scanner.nextInt() test if the entered value is a Java int actually, but Java ints are a subset of integers.</autism>
    java.math.BigInteger(String) can do it.

  8. The Following User Says Thank You to Sean4u For This Useful Post:

    MiniatureBeast (August 17th, 2011)

  9. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to make Java determine if a values is an integer or not.

    Thanks I'll look into this. Thanks to everyone else as well

Similar Threads

  1. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM
  2. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  3. [SOLVED] Help; algorithm to determine 'range'
    By b_jones10634 in forum Algorithms & Recursion
    Replies: 13
    Last Post: August 24th, 2010, 04:57 PM
  4. make dir using mkdir() in java io
    By mssi in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 6th, 2010, 04:51 PM
  5. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM

Tags for this Thread