Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From string to integer .....

Status
Not open for further replies.

javaken

Technical User
May 15, 2004
20
BE
Suppose: you have folowing string of numbers ....

"1 2 3 4 5 6 "

How to convert it to an integer?? The procedure will also be possible if your string exists of 10 numbers .... "1 2 3 4 5 6 7 8 9 10 "

Thanks
 
You could use StringTokenizer. StringTokenizer interprets the whitespace between each character as a break. So all you have to do is run the string of numbers in a loop that breaks each number into a token (which is actually a string) then run Integer.parseInt(token) on each one. You can find references to StringTokenizer just about anywhere. I'm a little rusty at it but I think it may go like this:

Code:
int num;
String s = "1 2 3 4 5"; // String of numbers here
String token;
StringTokenizer st = new StringTokenizer(s);

token = st.nextToken; // Store first number in token

while(st.hasMoreTokens()){
  num = Integer.parseInt(token); // Convert string to int
  System.out.println(num); // Print int
  token = st.nextToken; // Move to next number in string
}

Good Luck,
Jisoo23
 
Oops, make sure to add "import java.util.*;" at the top.
 
Ooops!
a) nextToken is method, needs ();
b) wouldn't work, if the String is "1" or " 17 "

Code:
// nextToken wrong here

while (st.hasMoreTokens ())
{
	token = st.nextToken (); 
	num = Integer.parseInt (token);
	System.out.println (num); 
}

seeking a job as java-programmer in Berlin:
 
stefan :

You using a StringTokenizer ? I thought you were always a fan of String.split() ?! :)

Code:
String s = "1 2 3 4 5";
String[] parts = s.split(" ");
for (int i = 0; i < parts.length; i++) {
  int j = Integer.parseInt(parts[i]);
  System.err.println(j);
}
 
I was used to StringTokenizer and used it quiet often.
Since a few weeks, I know sun is discouraging the usage of ST (without deprecating it) and will prefer 'split' in future.

But this posting was a correction of jisoo23, so I tried to stay close to his example.

Of course I prefer scanner:

Code:
Scanner sc = Scanner.create (s);
while (sc.hasNext ()) 
{
	int i = sc.nextInt();
	System.out.println ("i="+i); 
}
(java 1.5 beta)


seeking a job as java-programmer in Berlin:
 
Cool - I have not had the time to try out 1.5 yet ...
 
What if your inputstring is null??? Because it becomes from a object ....

java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)



 
Woo, I haven't seen scanner yet. Looks like it's time to try 1.5 beta! My school always tossed stringtokenizer at me which is why I'm sorta used to using it =/ But looking like I'll move split or scanner, whole lot less code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top