String Integer Input check

Discussion in 'Java' started by Paul Wagener, Nov 14, 2004.

  1. Paul Wagener

    Paul Wagener Guest

    Hi folks,

    Problem:
    User is expected to input a String type.
    How to check if the input is valid and not of Integer type?

    Thanks a lot!
    Paul
     
    Paul Wagener, Nov 14, 2004
    #1
    1. Advertising

  2. Paul Wagener

    Dave Neary Guest

    Hi,

    On Sun, 14 Nov 2004 22:03:55 +0100, Paul Wagener said:
    > Problem:
    > User is expected to input a String type.
    > How to check if the input is valid and not of Integer type?


    Every string which can be parsed to an integer is a valid
    String...

    To check whether you can parse a string as an integer, try to
    parse it

    int i;
    String s = "132";
    try {
    i = Integer.parseInt(s);
    } catch (NumberFormatException nfe) {
    System.err.println("s is not parseable as an integer");
    }

    Cheers,
    Dave.

    --
    David Neary,
    E-Mail: bolsh at gimp dot org
    Work e-mail: d dot neary at phenix dot fr
    CV: http://dneary.free.fr/CV/
     
    Dave Neary, Nov 14, 2004
    #2
    1. Advertising

  3. Paul Wagener

    VisionSet Guest

    "Paul Wagener" <> wrote in message
    news:4197c836$...
    > Hi folks,
    >
    > Problem:
    > User is expected to input a String type.
    > How to check if the input is valid and not of Integer type?


    Integer as text is a subset of String, that aside...

    You probably don't want to throw exceptions if you expect it to be a String.
    So do myString.toCharArray(), then step through and check each char with
    Character.isDigit(myChar)

    --
    Mike W
     
    VisionSet, Nov 14, 2004
    #3
  4. VisionSet wrote:
    > You probably don't want to throw exceptions if you expect it to be a String.
    > So do myString.toCharArray(), then step through and check each char with
    > Character.isDigit(myChar)


    Note that Character.isDigit() returns true for quite a lot of digit characters
    used in other languages.
     
    Michael Borgwardt, Nov 15, 2004
    #4
  5. Paul Wagener

    Eric Sosman Guest

    VisionSet wrote:
    > "Paul Wagener" <> wrote in message
    > news:4197c836$...
    >
    >>Hi folks,
    >>
    >>Problem:
    >>User is expected to input a String type.
    >>How to check if the input is valid and not of Integer type?

    >
    >
    > Integer as text is a subset of String, that aside...
    >
    > You probably don't want to throw exceptions if you expect it to be a String.
    > So do myString.toCharArray(), then step through and check each char with
    > Character.isDigit(myChar)


    That might be useful as a pre-screening step, but
    is certainly not definitive. For example, what would
    the scan conclude about "999999999999999999999999999"?
    Or about "-42", for that matter?

    --
     
    Eric Sosman, Nov 15, 2004
    #5
  6. Paul Wagener

    VisionSet Guest

    "Eric Sosman" <> wrote in message
    news:cnajf3$1b7$...

    > > So do myString.toCharArray(), then step through and check each char with
    > > Character.isDigit(myChar)

    >
    > That might be useful as a pre-screening step, but
    > is certainly not definitive. For example, what would
    > the scan conclude about "999999999999999999999999999"?
    > Or about "-42", for that matter?


    The problem space was far from clearly defined, my solution was equal in
    this regard the OP can glean from it what he will.

    --
    Mike W
     
    VisionSet, Nov 15, 2004
    #6
  7. Paul Wagener

    Ann Guest

    "Paul Wagener" <> wrote in message
    news:4197c836$...
    > Hi folks,
    >
    > Problem:
    > User is expected to input a String type.
    > How to check if the input is valid and not of Integer type?
    >
    > Thanks a lot!
    > Paul

    -------------------
    If there is a '-', it must be first.
    All other chars must be digits.
    -------------------
    public class TryInt {
    public static void main(String[] args) {
    String numStr = "-7898787";
    int numInt = 0;
    int start = 0;
    boolean flag = false;
    System.out.println("Input: \"" + numStr);

    if (numStr.charAt(0) == '-') {
    flag = true;
    start = 1;
    }

    for (int i = start; i < numStr.length(); i++) {
    char a = numStr.charAt(i);
    if (Character.isDigit(a)) {
    numInt = numInt * 10 + a - '0';
    }
    else {
    System.err.println("Invalid integer character \'"
    + a + "\' at position " + i);
    System.exit(1);
    }
    }
    if (flag) numInt = -numInt;
    System.out.println("Output: \"" + numInt);
    }
    }
     
    Ann, Nov 15, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig Deelsnyder
    Replies:
    0
    Views:
    710
    Craig Deelsnyder
    Jul 28, 2003
  2. kt
    Replies:
    3
    Views:
    40,323
    Betty
    Apr 22, 2005
  3. Replies:
    11
    Views:
    570
  4. Replies:
    5
    Views:
    984
    X-Centric
    Jun 30, 2005
  5. Randy Kramer
    Replies:
    12
    Views:
    407
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page