• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Reader Problem  RSS feed

 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all! I'm having an odd problem I can't seem to figure out - I have a method that reads the input from a file, converts it to a string array, then to string to tokenize, then to a 2D string array. I've initialized both arrays based on a buffered reader determining the number of lines in the file using the readLine method. Here are the appropriate portions of code:



And the information in the text file is:

"11212,Written Assignment 1,Math Test,Test Notes,false,
1234,New Assignment,English Test,This was added and refreshed!,false,"

The reader always returns 4 lines. Any idea why? Thanks!

EDIT: Realized I posted the wrong method, corrected this...
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Pettit wrote:The reader always returns 4 lines. Any idea why? Thanks!

How did you confirm that? I tried with the text file you gave, and it returned me only two.

The issue seems to be in the Tokenizer loop.

Note that array variables range is from 0 - (n-1); where n is the array length. Here since b is initialized to hold two arrays and the index 2 is accessed in the loop (0, 1, and then 2) ArrayIndexOutOfBoundsException is thrown.

Once you alleviate the issue, you can test the tokens stored by printing them. Below is a sample code to print them.

 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
Joe Pettit wrote:The reader always returns 4 lines. Any idea why? Thanks!

How did you confirm that? I tried with the text file you gave, and it returned me only two.

The issue seems to be in the Tokenizer loop.

Note that array variables range is from 0 - (n-1); where n is the array length. Here since b is initialized to hold two arrays and the index 2 is accessed in the loop (0, 1, and then 2) ArrayIndexOutOfBoundsException is thrown.

Once you alleviate the issue, you can test the tokens stored by printing them. Below is a sample code to print them.



I checked the number of lines by adding a println statement printing the variable that holds the number of ines - assigNumRows. I'll take a look at the tokenizer loop - thank you!
 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error seems to be thrown in this for loop:



I got the count showing the right value, but this doesn't seem to want to work. It seems to be an error with the nextLine method - is there anything (like spaces, etc.) that might throw off this method? It pulls the first line from the file, but throws the error on the second line.

Thanks!
 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Pettit wrote:The error seems to be thrown in this for loop:



I got the count showing the right value, but this doesn't seem to want to work. It seems to be an error with the nextLine method - is there anything (like spaces, etc.) that might throw off this method? It pulls the first line from the file, but throws the error on the second line.

Thanks!


Nevermind! Figured it out - worked just fine with a while loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!