File Handling Problems


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: File Handling Problems

  1. #1
    Join Date
    Apr 2004
    Posts
    8

    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

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    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

  3. #3
    Join Date
    Apr 2004
    Posts
    8

    Update

    Unfortunately I don't have control over the layout of the file. The text has to be processed as it stands.

    Cheers,
    Munki

  4. #4
    Join Date
    Apr 2004
    Posts
    8

    Thank you

    Thank you Reinkesm for your help the hasNextToken() method worked.

    Thanks again for the help,
    Munki

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    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)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center