Results 1 to 14 of 14
- 04-01-2008, 11:48 AM #1
[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 12:17 PM.
Did this post help you? Please
me! :cool:
-
Just remove the while loop and keep only the for loop.
- 04-01-2008, 12:02 PM #3
Thank you Java Tip.
Did this post help you? Please
me! :cool:
- 04-01-2008, 12:08 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
:) :)
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 :(
- 04-01-2008, 12:15 PM #5
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:
- 04-01-2008, 12:17 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oops, you are writing that FAQ/Rules page. Sorry if I make any trouble to you. :)
- 04-01-2008, 12:18 PM #7
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:
- 05-20-2010, 10:15 AM #8
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
It helped me....thank you very much :D
- 05-20-2010, 05:09 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-05-2010, 01:58 AM #10
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
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
- 06-05-2010, 02:28 AM #11
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
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(",");
}
- 06-05-2010, 11:39 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-25-2011, 12:48 AM #13
Member
- Join Date
- Jan 2011
- Location
- Baguio City, Philippines
- Posts
- 21
- Rep Power
- 0
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.
-
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
-
Reading Integers from a text file
By tress in forum New To JavaReplies: 6Last Post: 02-26-2011, 05:45 PM -
[SOLVED] getting values from a text file
By dav9999 in forum New To JavaReplies: 8Last Post: 04-01-2008, 01:51 AM -
Applet - reading text files packed into JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:15 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


LinkBack URL
About LinkBacks


Bookmarks