check if integer or string

how can i check if the current token is integer or stringsuppose i have Mary 33 London 410how can i check if this token is integer to do something or string to do somethingelse?
[205 byte] By [TheNewLeadera] at [2007-11-27 4:53:56]
# 1
1. get it as a string 2a. convert to an int with: parseInt(String s) throws NumberFormatException2b. or convert to an Integer with: valueOf(String s) throws NumberFormatException
corlettka at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 2

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

NourElsaftya at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 3
Ahmad,I didn't know that one.... Cool !!! Thanking you sir, Keith.
corlettka at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks AhmadBut it didn't work even i have import the followingimport java.util.Scanner;import java.io.*it said can't find symbol methods.hasNext())if (s.hasNextInt())int number = s.nextInt();why?
TheNewLeadera at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 5
You can refere to Scanner class documentation: http://java.sun.com/javase/6/docs/api/java/util/Scanner.htmlit realy has a method called public boolean hasNext().
NourElsaftya at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks for the link Ahmad,the error has gone ;-) but i can't see any result ;-(
TheNewLeadera at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 7
you did not see any results because you simply did not print out any output..........right?
NourElsaftya at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 8

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("");

}

TheNewLeadera at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 9

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

NourElsaftya at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 10
It is ok brother(惹堰 轻徨 蓓?嬉窍?卺闱)Thanks again ;-)
TheNewLeadera at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 11
I have a dialog that asks for a user's telephone number. Here is a sample input07912065462Can you tell if that's a number or a String?
georgemca at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 12
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.
lethalwirea at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 13

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?

TheNewLeadera at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 14
Hi allno answer for the file and repition
TheNewLeadera at 2007-7-12 10:08:30 > top of Java-index,Java Essentials,Java Programming...
# 15
If the ouput is repeated in the file then your code must be outputting it multiple times. Check your code where it performs the file writing.
floundera at 2007-7-21 21:15:43 > top of Java-index,Java Essentials,Java Programming...
# 16

Sorry for late.

>does it read from a file?

yes, it reads from a file, refere to Scanner class documentation page: http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

I can not understand what you mean from the rest of your message?

Please more declaration about your situation.

Thanks.

Ahmad Elsafty

NourElsaftya at 2007-7-21 21:15:43 > top of Java-index,Java Essentials,Java Programming...