1 2 Previous Next 16 Replies Latest reply: May 25, 2007 3:09 PM by 807606 RSS

    check if integer or string

    807606
      how can i check if the current token is integer or string

      suppose i have

      Mary 33 London 410

      how can i check if this token is integer to do something or string to do somethingelse???
        • 1. Re: check if integer or string
          800308
          1. get it as a string
          2a. convert to an int with: parseInt(String s) throws NumberFormatException
          2b. or convert to an Integer with: valueOf(String s) throws NumberFormatException
          • 2. Re: check if integer or string
            807606
            You can use the Scanner class which introduced in Java 5.
            here is a code sample:
            String s = "Mary 33 London 410";
            Scanner sc = new Scanner(s);
            while (s.hasNext())
            {
                 if (s.hasNextInt())
                 {
                       int number = s.nextInt();
                       // do something......
                 }
                 // .... and so............
            }
            Good Luck
            Ahmad Elsafty
            • 3. Re: check if integer or string
              800308
              Ahmad,

              I didn't know that one.... Cool !!!

              Thanking you sir, Keith.
              • 4. Re: check if integer or string
                807606
                Thanks Ahmad

                But it didn't work even i have import the following

                import java.util.Scanner;
                import java.io.*

                it said can't find symbol method
                s.hasNext())
                if (s.hasNextInt())
                int number = s.nextInt();

                why????
                • 5. Re: check if integer or string
                  807606
                  You can refere to Scanner class documentation:
                  http://java.sun.com/javase/6/docs/api/java/util/Scanner.html
                  it realy has a method called public boolean hasNext().
                  • 6. Re: check if integer or string
                    807606
                    Thanks for the link Ahmad,

                    the error has gone ;-) but i can't see any result ;-(
                    • 7. Re: check if integer or string
                      807606
                      you did not see any results because you simply did not print out any output..........right?
                      • 8. Re: check if integer or string
                        807606
                        No i have the following sentece to be written but it didn't work too;-(
                        String s = "Mary 33 London 410";
                        Scanner sc = new Scanner(s);
                        
                        while (sc.hasNext() )
                        {
                               //System.out.println("Before IF ELSE");
                            if (sc.hasNextInt())
                             {
                                             int number = sc.nextInt();
                                   System.out.println("number");
                                   // do something......
                             }
                             // .... and so............
                             else
                                System.out.println("");
                        }
                        • 9. Re: check if integer or string
                          807606
                          oops, I am sorry, That's my fault, I ahave not explained to you.
                          Scanner class has many methods called hasNextXxx() each one just checks for the existing of the next token of the desired type, and not iterate over the next token, the method which gets the next token is nextXxx().

                          the following code should work(�� ��� ����):
                          import java.util.Scanner;
                          
                          public class ScannerTest
                          {
                               public static void main(String args[]) {
                                   String s = "Mary 33 London 410";
                                    Scanner sc = new Scanner(s);
                                     
                                    while (sc.hasNext() )
                                    {
                                           //System.out.println("Before IF ELSE");
                                        if (sc.hasNextInt())
                                         {
                                                 int number = sc.nextInt();
                                               System.out.println("number: " + number);
                                               // do something......
                                         }
                                         // .... and so............
                                         else
                                         {
                                              String str = sc.next();
                                              System.out.println("string: " + str);
                                         }
                                            
                                    }
                              }
                          }
                          Good Luck
                          Ahmad Elsafty
                          • 10. Re: check if integer or string
                            807606
                            It is ok brother
                            (���� ���� ��� ����� ����)

                            Thanks again ;-)
                            • 11. Re: check if integer or string
                              807606
                              I have a dialog that asks for a user's telephone number. Here is a sample input
                              07912065462
                              Can you tell if that's a number or a String?
                              • 12. Re: check if integer or string
                                807606
                                What kind of "dialog" is it?

                                If you're reading from data files, you can usually parse the String to an Integer value.
                                Or just read the number using Scanner. But the Scanner still reads the value as a String first, then parses.
                                • 13. Re: check if integer or string
                                  807606
                                  more question about the scanner

                                  does it read from a file??
                                  i have something strange when i try to save the output to a file
                                  it does repeate it more thatn 20 times and each time in each row
                                  the data is repeate one more i.e
                                  first row
                                  Mary 33 London 410
                                  second row
                                  Mary 33 London 410 Mary 33 London 410
                                  third row
                                  Mary 33 London 410 Mary 33 London 410 Mary 33 London 410
                                  and so on,
                                  any ideas why this is happings????
                                  • 14. Re: check if integer or string
                                    807606
                                    Hi all

                                    no answer for the file and repition
                                    1 2 Previous Next