Results 1 to 7 of 7
  1. #1
    Alk
    Alk is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Detecting if string or int

    Hello everyone, I'm pretty new to Java, and I was wondering if anyone could tell me how to see if an input is a string when it is supposed to be an int and then display an error message.

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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.

  3. #3
    Alk
    Alk is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Thumbs up

    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.

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    [B]try {[/B]
       z = input.nextInt();
    [B]} catch (InputMismatchException e) { e.printStackTrace(); }[/B]
    There you go.

  5. #5
    Alk
    Alk is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    I probably sound like an idiot, but where do I put in the code? I haven't looked into try-catch that much yet.

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Alk View Post
    I probably sound like an idiot, but where do I put in the code? I haven't looked into try-catch that much yet.
    Put it around your nextInt() line. Read up on Exceptions.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    A comment on your code:
    nextInt will read in a 0 or 00 or 000 as a 0. No way to test if the user entered: 0 or 00 or 0000

Similar Threads

  1. Detecting a lost X connection
    By jeffpaulwilson in forum AWT / Swing
    Replies: 2
    Last Post: 03-09-2011, 07:54 PM
  2. detecting folder listing
    By antiC in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 12:13 AM
  3. Detecting software installed in PC
    By Lukalo in forum Advanced Java
    Replies: 3
    Last Post: 02-13-2009, 03:04 AM
  4. detecting flooding attack
    By prashant in forum Networking
    Replies: 1
    Last Post: 12-27-2008, 08:44 PM
  5. Detecting Browser Settings
    By arupranjans in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-31-2007, 02:31 PM

Posting Permissions

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