Results 1 to 7 of 7
Thread: Detecting if string or int
- 05-14-2011, 06:08 PM #1
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
- 05-14-2011, 06:28 PM #2
How are you reading the input?
Do you understand what a character set is? Each character has an int value. Considering single byte characters, For ASCII there are about 120 different values (about the same as what you can enter from the keyboard). The digits are in one range of values. Letters are in other ranges of values for upper case and lower case. The Character class has methods that will tell you about the type of a character: Digit, letter, etc.
- 05-14-2011, 07:27 PM #3
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
I'm using Scanner to read the input.
I set up a sentinel like this:
Java Code:int z; boolean b = true; Scanner input = new Scanner(System.in); while(b){ System.out.println("Enter 1 to continue, or 00 to exit") z = input.nextInt(); if(z == 1){ //code }else if(z == 00){ b = false; } }
I just want to know how to display an error message and continue on with the loop if a string or a non-integer number was entered for z.Last edited by Alk; 05-14-2011 at 07:37 PM.
- 05-14-2011, 07:40 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
There you go.Java Code:[B]try {[/B] z = input.nextInt(); [B]} catch (InputMismatchException e) { e.printStackTrace(); }[/B]
- 05-14-2011, 07:56 PM #5
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
I probably sound like an idiot, but where do I put in the code? I haven't looked into try-catch that much yet.
- 05-14-2011, 08:09 PM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 05-14-2011, 11:26 PM #7
Similar Threads
-
Detecting a lost X connection
By jeffpaulwilson in forum AWT / SwingReplies: 2Last Post: 03-09-2011, 07:54 PM -
detecting folder listing
By antiC in forum New To JavaReplies: 1Last Post: 02-17-2011, 12:13 AM -
Detecting software installed in PC
By Lukalo in forum Advanced JavaReplies: 3Last Post: 02-13-2009, 03:04 AM -
detecting flooding attack
By prashant in forum NetworkingReplies: 1Last Post: 12-27-2008, 08:44 PM -
Detecting Browser Settings
By arupranjans in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-31-2007, 02:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks