4 Replies Latest reply: Dec 4, 2006 9:53 AM by 800282 RSS

    Filtering Strings

    807607
      I have a list of Strings some are "names" others are "numbers".

      1
      2
      3
      dave
      4
      john
      5
      Peter

      I want to filter the list so that only strings with the names remain.

      are there any mechanisms in java to do this?
        • 1. Re: Filtering Strings
          PhHein
          You could parse the Strings into a number, if that works, get rid of it, if not it's a name.
          • 2. Re: Filtering Strings
            800282
            I want to filter the list so that only strings with
            the names remain.

            are there any mechanisms in java to do this?
            Try parsing the Strings into an int by using
            Integer.parseInt(theString);
            , if a NumberFormatException is being thrown, it wasn�t a number.
            • 3. Re: Filtering Strings
              800329
              Hm, if you are sure, the incoming values are either numbers or Strings, the parseInt approach might help (although, it will throw exceptions on long numbers that exceed Integer.MAX_INT). Otherwise, better use a Pattern instance and match for digits. Might also pick out illegal names.
              • 4. Re: Filtering Strings
                800282
                Hm, if you are sure, the incoming values are either
                numbers or Strings, the parseInt approach might help
                (although, it will throw exceptions on long numbers
                that exceed Integer.MAX_INT). Otherwise, better use a
                Pattern instance and match for digits. Might also
                pick out illegal names.
                Good catch Stefan.

                @OP: for a pattern approach, have a look at this link:
                http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html