Using bufferedReader to write contents of text file into an object array.
I'm fairly new to JAVA and taking a class. One of the big parts of my assignment is to read from a text file and write those values to an array.
For the most part, I got it to work where it will read in the values and my object array will populate.
The text file is along these lines (item name and corresponding price...first line shows number of items):
6
item1 250
item2 350
item3 1050
item4
item5 1610
item6 1505
My problem comes about when it hits that "item4" line. I want it to not only add that item to my array, but to assign a value of "0" for that missing data. When I go into the text file and put in that zero, everything works beautifully. When I run it with that blank, it will skip the line entirely. I know that every time there is a "null" it throws me into NoSuchElementException, but I just don't know how to handle that blank the way I need it to.
My code for this section is as follows:
Code :
public void readArray (String fileName)
{
File inFile = new File(fileName);
BufferedReader bufReader;
FileReader fileReader;
StringTokenizer tokenizer;
String line, item;
double price;
int i =0;
try
{
fileReader = new FileReader(inFile);
bufReader = new BufferedReader(fileReader);
Scanner scan = new Scanner(inFile);
n = scan.nextInt();
stuff = new Inventory[n];
while((line = bufReader.readLine()) != null)
{
tokenizer = new StringTokenizer(line);
try
{
while(true)
{
item = tokenizer.nextToken();
price = Double.parseDouble(tokenizer.nextToken());
stuff[i] = new Inventory(item,price);
i++;
}
}
catch(NoSuchElementException exception)
{
//what do I even do here?
}
}
}
catch(FileNotFoundException exception)
{
System.out.println("Unable to open file. Check if the file exists or path is correct" +inFile.getAbsolutePath());
System.exit(0);
}
catch(IOException exception)
{
System.out.println("Could not open input file" +exception.getMessage());
}
}
I even tried just doing
stuff[i] = new Inventory(item,0);
just to see if it would pick up that row, and it doesn't. It will assign a 0 to every item, but completely skip item4.
Thanks guys!
Re: Using bufferedReader to write contents of text file into an object array.
The StringTokenizer has a method you should call to test if there is a next token before calling the nextToken() method. If there is no token, use the default value for the missing token.
Re: Using bufferedReader to write contents of text file into an object array.
I had tried to do it with "hasMoreTokens" but I couldn't get it to work correctly. It would still throw that "NoSuchElementException" and throw it out of the loop before it even writes to the array...how would I successfully implement something like that?
Re: Using bufferedReader to write contents of text file into an object array.
You need to check for more tokens for EVERY time you call nextToken().
Code :
if(has more) {
OK to use nextToken()
}
if(has more) {
Ok to use nextToken()
}
etc
Re: Using bufferedReader to write contents of text file into an object array.
I changed my loop to the following:
Code :
while((line = bufReader.readLine()) != null)
{
tokenizer = new StringTokenizer(line);
more = tokenizer.hasMoreTokens();
try
{
while(true)
{
item = tokenizer.nextToken();
if (more = true)
{
price = Double.parseDouble(tokenizer.nextToken());
stuff[i] = new Inventory(item,price);
}
if (more = false)
{
stuff[i] = new Inventory(item);
}
i++;
}
}
I still get the same problem, however, where it entirely skips that line that doesn't have the second element. Did I not implement that correctly?
Re: Using bufferedReader to write contents of text file into an object array.
You have to reevaluate tokenizer.hasMoreToken() each time you use the tokenizer.nextToken() method.
Explain to me the reason you have the while loop (the one that says while(true)).
Re: Using bufferedReader to write contents of text file into an object array.
Quote:
Did I not implement that correctly?
Yes, you did not implement the logic correctly.
Re: Using bufferedReader to write contents of text file into an object array.
I had the while (true) loop to fill the array. I had tried to use a for loop, but every time it would hit that null at the end of the line, it would go to the noSuchElementException and initialize the i count back to zero...essentially my array kept overwriting itself. I would prefer the for loop to fill the array, but I saw an example somewhere where they use the while loop and it gave me less errors. What would be the best way to handle this using a for loop?
Re: Using bufferedReader to write contents of text file into an object array.
Code :
if(has more) {
OK to use nextToken()
}
if(has more) {
Ok to use nextToken()
}
etc
You need a way to exit the loop. Use a break statement when there are no more tokens.
Re: Using bufferedReader to write contents of text file into an object array.
Ok, tell me if I am wrong, but here's how I would expect your original code to run:
Code :
ENTER WHILE (outer):
line = "item1 250"
tokenizer = new StringTokenizer(line);
ENTER TRY:
ENTER WHILE (inner):
item = "item1"
price = 250.0
stuff[0] = new Inventory(item,price);
CONTINUE WHILE (inner):
item = tokenizer.nextToken() == NoSuchElementException
ENTER CATCH:
do nothing
CONTINUE WHILE (outer):
line = "item2 350"
tokenizer = new StringTokenizer(line);
ENTER TRY:
ENTER WHILE (inner):
item = "item2"
price = 350.0
stuff[1] = new Inventory(item,price);
CONTINUE WHILE (inner):
item = tokenizer.nextToken() == NoSuchElementException
ENTER CATCH:
do nothing
CONTINUE WHILE (outer):
line = "item3 1050"
tokenizer = new StringTokenizer(line);
ENTER TRY:
ENTER WHILE (inner):
item = "item3"
price = 1050.0
stuff[2] = new Inventory(item,price);
CONTINUE WHILE (inner):
item = tokenizer.nextToken() == NoSuchElementException
ENTER CATCH:
do nothing
CONTINUE WHILE (outer):
line = "item4"
tokenizer = new StringTokenizer(line);
ENTER TRY:
ENTER WHILE (inner):
item = "item4"
price == NoSuchElementException
ENTER CATCH:
do nothing
CONTINUE WHILE (outer):
line = "item5 1610"
tokenizer = new StringTokenizer(line);
ENTER TRY:
ENTER WHILE (inner):
item = "item5"
price = 1610.0
stuff[3] = new Inventory(item,price);
CONTINUE WHILE (inner):
item = tokenizer.nextToken() == NoSuchElementException
ENTER CATCH:
do nothing
CONTINUE WHILE (outer):
line = "item6 1505"
tokenizer = new StringTokenizer(line);
ENTER TRY:
ENTER WHILE (inner):
item = "item6"
price = 1505.0
stuff[4] = new Inventory(item,price);
CONTINUE WHILE (inner):
item = tokenizer.nextToken() == NoSuchElementException
ENTER CATCH:
do nothing
EXIT WHILE (outer)
Now, with the while(true) loop, you would just be getting a ton of NoSuchElementExceptions since you would continue to try to tokenize past the end of the line. If I'm correctly, you probably wouldn't even notice, since you are catching those errors, but doing nothing when you do. What I mean by all of this is that the inner while loop (or any inner loop) is unnecessary in this situation (assuming you don't have more than one Inventory Item on each line).