-
File Handling Problems
OK, I am having problems reading text from a file. The text in the folder takes the following form.
0.01 0.01 191650 5 | ae United Arab Emirates
0.01 0.01 146570 8 | am Armenia
I only wish to read data from the third, fourth, sixth and seventh column. Curerntly I have been using this code to read all the data.
data = inFile.readLine();
while(data != null)
{
StringTokenizer st = new StringTokenizer(data);
valA = st.nextToken();
valB = st.nextToken();
valC = st.nextToken();
valD = st.nextToken();
valE = st.nextToken();
valF = st.nextToken();
valG = st.nextToken();
valH = st.nextToken();
valI = st.nextToken();
System.out.println(valF+" "+valC+" "+valD+" "+valG+
" "+valH+" "+valI);
data = inFile.readLine();
}
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
-
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.
-- Steven
-
Update
Unfortunately I don't have control over the layout of the file. The text has to be processed as it stands.
Cheers,
Munki
-
Thank you
Thank you Reinkesm for your help the hasNextToken() method worked.
Thanks again for the help,
Munki
-
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)
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks