#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14

    Reading a textfile into lines


    *sigh*

    I have another problem with Java. This time, I'm reading in a file. I'm actually locating the file, opening the reader, reading in the characters and outputting them as characters, but I cannot seem to detect the newline character. Can anyone help me out here?

    I have a text file that has several lines of text that need to be inserted into objects. Until I can break up the lines of text, I cannot continue.

    HELP!

    Statik
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    68
    Rep Power
    14
    the DataInputStream's readLine() method does well with that if that's how you are reading the text file?

    did you try breaking at '\n' ?
  4. #3
  5. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    We need some code Statik
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    I was using file and read, which reads in char datatypes and \n is a string. I'll try changing to datainputstream and use readline and see how that goes. Thanks

    Statik
  8. #5
  9. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    I doubt this is what you need, but I'm going to go ahead and post it anyway .

    You can get whatever the local system thinks should be used for seperating lines with System.getProperty("line.separator").
    -james
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    OK, I'm reading the file, and putting the values into the object. Unfortunately, I can't seem to get them back out. I'll post the two java files and the data file. Can anyone see where I've erred?
    Attached Files
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    Here's the student object
    Attached Files
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    And the data file
    Attached Files
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    Note that the file about should be renamed to students.txt

    Statik
  18. #10
  19. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, I don't know if this is your problem or not, but that text file is empty.

    Your code look fine, though.
    -james
  20. #11
  21. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    When I make a dummy student.txt file (yours is empty) and run your code, I get null pointer exceptions because you never add any real objects to your studentlist array. You make the array, but all that does is make an array of references to student objects, it does not actually make any real student objects.

    By adding something like:
    Code:
    for (int i = 0; i < studentlist.length; i++) {
    	studentlist[i] = new student();
    }
    after the array is created, the program runs to completion for me.
    Last edited by Nemi; February 15th, 2003 at 01:24 PM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    Doh! *slaps forehead* I shoulda known that creating the array of bjects didn't create the objects.

    Thanks
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    130
    Rep Power
    14
    Doh! *slaps forehead* I shoulda known that creating the array of bjects didn't create the objects.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo