Results 1 to 2 of 2
- 05-08-2010, 04:57 PM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
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?
- 05-08-2010, 05:14 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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
-
finding max and min with inheritance with two input integer
By drmarx in forum New To JavaReplies: 3Last Post: 12-04-2009, 05:21 PM -
how to know the input value of integer
By ran830421 in forum New To JavaReplies: 15Last Post: 11-18-2009, 09:01 PM -
Input technique for unknown lines of input
By ducreative in forum New To JavaReplies: 16Last Post: 09-23-2009, 09:26 AM -
input to an integer (simply)
By chitwood in forum Advanced JavaReplies: 3Last Post: 03-18-2009, 06:34 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks