
November 30th, 2012, 07:29 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 6 m 48 sec
Reputation Power: 0
|
|
|
Homework - Java ArrayList / File Input Help
Hi, I'm writing a program for a Computer Science project and am having trouble. The program is dealing with creating a custom class (Height) with two private int variables (pounds, ounces). When working with the main file (HeightsSorter) I am trying to read a file for input, create Height objects from each line of input and add them to an existing ArrayList that I have created. I can't seem to figure this out. For some reason it doesn't seem to be adding any of my Height objects to my ArrayList. If anyone could help me it would be much appreciated!
Here is the code I have for my main class:
Scanner keyb = new Scanner(System.in);
System.out.print("Enter the file name> ");
String dataFile = keyb.next();
Scanner readFile = new Scanner(new File(dataFile));
int ft, in;
ArrayList<Height> heights = new ArrayList<Height>();
while (readFile.hasNextInt())
{
ft = readFile.nextInt();
in = readFile.nextInt();
heights.add(new Height(ft,in));
}
readFile.close();
int i;
System.out.println("Unsorted Names from "+dataFile+":");
for (i=0; i<heights.size();i++)
{
System.out.println(heights.get(i));
}
The output is this:
Enter the file name> heights.txt
Unsorted Names from heights.txt:
I am pretty sure that the file is right. I know that the name is. But I just don't know what I'm doing wrong!
Please help,
Addison
|