Results 1 to 2 of 2
  1. #1
    Mayday is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Question Testing if an input is an integer

    Hi, I'm writing a method that asks the user for an integer. The user can enter a string or anything they want. If the user enters anything but an integer, the method would repeatedly ask the user for an integer until the user enters one. I'm confused right now, how do I check if a string is an integer? :confused:
    Can anyone help?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Mayday View Post
    Hi, I'm writing a method that asks the user for an integer. The user can enter a string or anything they want. If the user enters anything but an integer, the method would repeatedly ask the user for an integer until the user enters one. I'm confused right now, how do I check if a string is an integer? :confused:
    Can anyone help?
    Sure, have a look at the Integer.parseInt( ... ) method; it throws a NumberFormatException if the String to be parsed is not a valid integer. Catch the Exception and try again. If I were you I'd build a separate method that prompts the user and tries over and over again if the user typed invalid input.

    kind regards,

    Jos

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2009, 05:21 PM
  2. how to know the input value of integer
    By ran830421 in forum New To Java
    Replies: 15
    Last Post: 11-18-2009, 09:01 PM
  3. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  4. input to an integer (simply)
    By chitwood in forum Advanced Java
    Replies: 3
    Last Post: 03-18-2009, 06:34 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •