I'm glad I found this forum. Just doing seraches has helped me already. However, I am having a problem with my assigment.
I want to open data.txt file, read the file line by line. Each line has four items, int, string, int, and double. Then put the items into an array, and then print out the items in the array.
At this time, the code reads one line only.
Here is what is in my data.txt file:
3176 battery 15 45.25
2217 tire 10 12.50
Here is my code. Any help is appreciated,
Jim
iimport java.io.*;
import java.util.*;
public class ReadLines{
public static void main(String args[]) {
int count = 0;
int MAX_LENGTH = 3;
int partID = 0;
String partName = " ";
int partStock = 0;
double partPrice = 0;
try{
BufferedReader in =
new BufferedReader(new FileReader ("data.txt"));// read file
CarPart[] array = new CarPart[MAX_LENGTH];//array
String line = in.readLine();//read a line in file
StringTokenizer st = new StringTokenizer(line);//tokenizer
if (line != null) {
partID = Integer.parseInt(st.nextToken());
partName = st.nextToken();
partStock = Integer.parseInt(st.nextToken());
partPrice = Double.parseDouble(st.nextToken());
CarPart cp = new CarPart(partID, partName, partStock, partPrice);
array[count] = cp;
count++;
}//end if
System.out.println("Part: " + partID +" " +partName + " " + partStock + " " + partPrice);
in.close();
}//end try
catch (Exception e) {
System.err.println(e); // Print the exception to warn.
}//end catch
}//end main
}//end class