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: How to check that console input should be integer only?

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to check that console input should be integer only?

    Hi there,

    I'm fairly new to java and am trying to validate user input to ensure what is entered is an integer.

    so I have.....

    int value = Keyboard.readInt();

    I'd like a loop of somesort that can check whether the input was an integer, as oppose to a float.

    Any ideas how you can do this? (baring in mind I'm still a beginner and am not familiar with more advanced ways to solve this problem).

    Many thanks


  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: How to check an input is an integer as oppose to a float

    Hello Konnor and welcome to the Java Programming Forums

    Here is a simple method for you:

    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!"); 
              }
     
        }
     
    }

    This uses the Scanner class to take user input from the console and then it tries to parse the given value to Integer.
    A try/catch block catches any exceptions which are generated when a non Integer value is entered.
    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
    Junior Member
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to check an input is an integer as oppose to a float

    Thanks for that.

    I'll have a look online and try to understand whats going on.

  4. #4
    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: How to check an input is an integer as oppose to a float

    Hey Konnor,

    Glad I could help...

    Check out the Sun Java Tutorials:

    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    The try Block (The Java™ Tutorials > Essential Classes > Exceptions)

    Basically when there is an error, this is called an 'exception'. You can catch these exceptions in a try/catch block.
    In the above code, when the code cannot parse the given value as an Integer, an error is generated. We have created the try/catch block to catch the NumberFormatException error which then prints "You have not entered an Integer!"

    When the given value is an actual Integer, no error is created so nothing goes to the catch part of the code.
    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.

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  3. [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
  4. 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
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM