String Tokenizing (but a little harder)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: String Tokenizing (but a little harder)

  1. #1
    Join Date
    May 2005
    Posts
    22

    String Tokenizing (but a little harder)

    I have to read inlines from a file. It goes like this:

    ID A1 A2 A3 A4 Midterm Final
    35 35 35 30 30 100
    133502 25 14.5 31.5 30 19.5 60
    154741 5 5 17 17 69.5
    157880 34 15 35 30 18 76.5
    158140 25 11 12

    and so on.
    The top line is just headers. The second is what everything is out of.
    Below that everything is layed out. Now this would be easy if all values were put in. However Every blank space is supposed to be a zero.

    So using a string tokenizer how would I be able to read in say lines 1 and two and get something like this.

    Student 1 has ID 133502, and marks [25,14.5,31.5,19.5,60]
    Student 2 has ID 154741, and marks [5,0,5,17,17,69.5]
    and so on?

    Using just basic String tokenization it would ofcourse skip the blank in student two and would only give me four marks. Any tips on how to do this? Atleast point me in the right direction^^ Thx

    Edit: I uploaded the txt file since it wasnt showing it right.
    Attached Files Attached Files
    Last edited by p1kn1c; 01-14-2006 at 03:56 PM.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    thats just a horrible way to format a file. slap your teacher! So a space acts as a delimiter but also represents a null value for a particular assignment? Only thing you can do in this case is "assume" that only 3 grades would mean the student only completed assignments A1-A3 and none of the later ones. If they only had a grade for A1 and A4 and spaces inbetween, it would be read in as A1 and A2 instead. If thats the way it's suppose to be, then:

    read a line
    create a new StringTokenizer with that line.
    read the first token as the "id"
    while hasMoreTokens(), read in a grade
    when done, read next line of file and create a new tokenizer again.

    You can read the first line of the file to determine the max number of grades then build an array of that size or just use ArrayList.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    As I see it this can't be handled consistently without some defined columns widths or delimiters (tabs ? ), ah, and yes... slap your teacher !
    eschew obfuscation

  4. #4
    Join Date
    May 2005
    Posts
    22
    Ya im going to email him about this ..maybe he meant that if theres a space then everything just slides over. I figured it was pretty much impossible.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    ...reminds of an IT "expert" I met once, he could not see the need for the newline character (it made the code look messy...), after all, when a text string hit the right side of the sceen it would wrap anyway...
    eschew obfuscation

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 03:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 07:12 AM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 10:16 PM
  4. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 08:36 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 04:21 AM

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