This works fine for the first row of data, but obviously fails on the second and throws an exception as there are less tokens in this row. Can anyone suggest a way of handling this data?
Cheers,
Munki
04-18-2004, 11:58 PM
reinkesm
You might be able to utilize the StringTokenizer method, hasMoreTokens().
But are you in control of the format of the file? The easiest solution would be to comma deliminate it and set the tokenizer based on that. so
Code:
0.01,0.01,191650,5,|,ae,United Arab Emirates
0.01,0.01,146570,8,|,am,Armenia
And use
Code:
StringTokenizer st = new StringTokenizer(data,",");
If you want to keep it spaced out for easier reading though, you could put quotes around anything that should be treated as one token, ie "United Arab Emirates" and code that into your parsing.
Just a few ideas.
04-19-2004, 06:13 AM
munki020
Update
Unfortunately I don't have control over the layout of the file. The text has to be processed as it stands.
Cheers,
Munki
04-19-2004, 06:32 AM
munki020
Thank you
Thank you Reinkesm for your help the hasNextToken() method worked.
Thanks again for the help,
Munki
04-21-2004, 08:38 AM
cjard
it might have been easier to do this:
Code:
String[] temp;
for(data = inFile.readLine();data != null;data = inFile.readLine()){
temp = data.split("\\s");
System.out.println("column 5 is: "+temp[4]+" and column 6 is: "+temp[5]);
}
you can say
if temp.length>5
to be sure that the array has at least 6 elements
remember that arrays start from 0, so every column index will be shifted down 1 (col 1 == array index 0 etc)
04-21-2004, 08:47 AM
cjard
oh, and if the columns are nice and regular, and only start to change length after a certain number, then you can use substring
by my count, there are 25 characters before the country name starts, so you can just do:
String countryname = data.substring(25);
and then spilt() the data, and pull the info you want from the other columns.. saves messing around joining the strings back together