Results 1 to 14 of 14
  1. #1
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Question [SOLVED] Reading a text file into an Array

    Hello.

    I have wrote some code to read a 10 lined text file line by line.
    As it is reads each line, the string is added to an Array.

    The only problem is, the code seems to be skipping the first line of the file.
    All the other lines are stored into the Array without a problem.

    Can someone please tell me how to fix this :confused: Thanks!

    Java Code:
    FileInputStream in = new FileInputStream("myfile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
    String strLine;
    String[] myarray;
    myarray = new String[10];
    		
    while ((strLine = br.readLine()) != null) {
    			
    for (int j = 0; j < myarray.length; j++){
    myarray[j] = br.readLine();
    }
    				
    }
    in.close();
    Last edited by DonCash; 04-01-2008 at 01:17 PM.
    Did this post help you? Please me! :cool:

  2. #2
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    10

    Default

    Just remove the while loop and keep only the for loop.

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Thank you Java Tip.
    Did this post help you? Please me! :cool:

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    :) :)

    Don you got the point, ha. Just look at the code, there you called the readLine(). It not buffered the content, and you miss the first line.

    At the end you don't have a line to read at all, NullPointerException :(

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Yeah Eranga, I knew the answer really :p

    I posted this so I could take some screenshots to add to my post about marking threads as Solved.
    Did this post help you? Please me! :cool:

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    No trouble at all!! You never know, this thread might help someone else in the future. :D
    Did this post help you? Please me! :cool:

  8. #8
    NeverHide is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    It helped me....thank you very much :D

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by NeverHide View Post
    It helped me....thank you very much :D
    If you got something from this, it's make us happy. :)

  10. #10
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    So what would be the correct way of writing this code? I'm having this problem but I don't have a while loop in my code.

    for (int compteur = 0; compteur < myArray.length; compteur++)
    {
    String thisword = br.readLine();
    myArray = thisword.split(",");
    }

    All variables are declared correctly, but if i do myArray[0], i'm actually getting the second line of my file. I tried to stick everything on one line and got a NullPointerException!

    Help?:D

  11. #11
    Frankybb is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Figured it out, I needed to remove the readLine(); from the loop entirely like so :

    String thisword = br.readLine();
    for (int compteur = 0; compteur < myArray.length; compteur++)
    {
    myArray = thisword.split(",");
    }

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Pleas do not post unrelated stuff in threads. If your question is not related with the thread, please start a new thread in the appropriate sub-forum.

  13. #13
    johnmergene is offline Member
    Join Date
    Jan 2011
    Location
    Baguio City, Philippines
    Posts
    21
    Rep Power
    0

    Lightbulb Hi.. i need help

    Example of java codes, student records?
    I need some codes in java programming for my students records management.The processes of displaying the records on the output screen, adding new records, searching from the record, editing fields of an existing record and other relevant processes should be involved. Since new records may be added when the program is executed, the program should include a way for the new set of data to be printed in a text file.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by johnmergene View Post
    Example of java codes, student records?
    I need some codes in java programming for my students records management.The processes of displaying the records on the output screen, adding new records, searching from the record, editing fields of an existing record and other relevant processes should be involved. Since new records may be added when the program is executed, the program should include a way for the new set of data to be printed in a text file.
    If you have a question, please don't hijack a thread, but rather start your own new thread. Also, most folks here don't appreciate homework dumps. If you have a homework question, please show that you've put in some effort to solve it including your code attempt, and ask a specific question. Don't just dump your assignment requirements into the forum as it will only make folks not want to help. I'm locking this thread but invite you again to create your own thread.

    Luck.

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 06:45 PM
  2. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 02:51 AM
  3. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:15 AM
  4. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 12:38 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
  •