Question:
Inventory java program only reads one line?
William
2015-04-19 17:46:36 UTC
I have an assignment from my class that asks me to read a .txt file, save the data in an array, then print out that data. I have a program that compiles, but when I run it, it does not print the first line of the .txt file, prints the second line, then does not print the third line, but stays in the run and freezes. Here is the driver program that I've come up with so far:

import java.io.*;
import javax.swing.*;
import java.text.*;
import java.util.StringTokenizer;

public class Inventory {

public static void main(String[] args) throws IOException {

BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
final int MAX = 500;
InventoryItem[] items = new InventoryItem[MAX];
StringTokenizer tokenizer;
String line, name, file = "inventory.txt";
int units, count = 0;
double price;
try {
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);
line = inFile.readLine();
while ((line = inFile.readLine()) != null) {
tokenizer = new StringTokenizer(line);

while (tokenizer.hasMoreElements()) {
name = tokenizer.nextToken();
try {
units = Integer.parseInt(tokenizer.nextToken());
price = Double.parseDouble(tokenizer.nextToken());
items[count++] = new InventoryItem(name, units, price);
}
catch (NumberFormatException exception) {
System.out.println("Error in input. Line ignored:");
System.out.println(line);
}
line = inFile.readLine();
}
}
inFile.close();
Three answers:
Bob
2015-04-19 18:15:29 UTC
I think that all the examples you can find using Buffered this, and Reader that are completely obsolete. If you're still being taught to use them then your teacher needs to get his/her head out of 1999 and into 2015.



There is a single class that will do everything you want, and it's really easy to use. It's called Scanner, and it will break a file down into tokens or lines, and if you have a String, it will break that down into tokens and lines, too.



I use it for 99% of my file reading needs, and so should you.



I normally use one Scanner to break a file down into lines, then used a second nested Scanner on the line I've just read to break it down further.



To break a file down into lines:

Scanner inFile = new Scanner (new File ("inputFile.txt"));

while (inFile.hasNextLIne()) {

.. String line = inFile.nextLine();

}



To break a line down into tokens:

Scanner inTokens = new Scanner (line);

Now you can use nextInt(), nextDouble(), etc, for numbers; inTokens.next() gives you the next word, and there's much more besides.



See: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html



If you're reading a file then you'll need to use try-catch in case the file doesn't exist, and if you're trying to parse numbers then try-catch for InputMismatchException which happens if the next token can't be cast into a number of the desired type for any reason.



PS: If you need to read input from the keyboard, then use:

Scanner keyboard = new Scanner (System.in);
John
2015-04-19 18:38:11 UTC
A command like this will read a line, counting how many you have in the while...loops then it would answer why line is skipped. The code has three .readLine() commands, two are skipped.

=================

line = inFile.readLine();

=================



import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.StringTokenizer;



public class Inventory {

public static void main(String[] args) throws IOException {

final int MAX = 500;

final String file = "inventory.txt";



InventoryItem[] items = new InventoryItem[MAX];

FileReader fr = new FileReader(file);

BufferedReader inFile = new BufferedReader(fr);

String line = "";

int count = 0;

while ((line = inFile.readLine()) != null) {

StringTokenizer tokenizer = new StringTokenizer(line);

while (tokenizer.hasMoreElements()) {

String name = tokenizer.nextToken();

try {

int unit = Integer.parseInt(tokenizer.nextToken());

double price = Double.parseDouble( tokenizer.nextToken());

items[count++] = new InventoryItem(name, unit, price);

} catch (NumberFormatException exception) {

System.out.println("Error in input. Line ignored:");

System.out.println(line);

}

}

}

inFile.close();



for (int i = 0; i < count; i++) {

System.out.println(items[i]);

}

}

}



class InventoryItem {

private String name;

private int unit;

private double price;



public InventoryItem(String name, int unit, double price) {

this.name = name;

this.unit = unit;

this.price = price;

}



@Override

public String toString() {

return String.format("%s %d $%,.2f", this.name, this.unit, this.price);

}

}
William
2015-04-19 17:47:07 UTC
for (int scan = 0; scan < count; scan++)

System.out.println(items[scan]);

}

catch (FileNotFoundException exception){

System.out.println("The file " + file + " was not found.");

}

catch (IOException exception){

System.out.println(exception);

}

stdin.read();

}

}

Thanks for any help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...