I'm trying to convert a String that I've read from the first line of a text file to integer
this is how:
String="8 12 10"

and this is what I'd like to have:

int x=8
int y=12
int z=10

parseInt doesn't work because it gives me some other number (perhaps the header of the text file or sth)
Plzzzzzzzz help!
thanx in advance

Recommended Answers

You want to use String.split. Use white space as the delimiter. That'll split your big String into three smaller Strings. Then you can use parseInt.

http://java.sun.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29

Lots of good samples out …

Jump to Post

All 3 Replies

thank you so much!
great help!

also you can use StringTokenizer();

String x = "8 2 10";
StringTokenizer st = new StringTokenizer(x);

int first = Integer.parseInt(st.nextToken()); // first = 8;

..... and so on. =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.