I'm having all sorts of problems getting this program to work. I think I know what's wrong with it, but I don't know how to fix the problem.
The program is supposed to read a text file that contains information like below....
1 99
2 88
3 77
4 66
5 55
1 10
2 35
5 50
3 60
It supposed to add up the numbers on the right, corresponding with the numbers on the left and write that information to a file. For example there are 105 "5"s and 123 "2"s.
So the new file should look like this
1 109
2 123
3 137
4 66
5 105
I have to do the writing with a Method, and that method has to be accessed 5 times....
I've been working on this little program for 2 days now, and I really don't think the teacher explained this.
The problem is...when I open my newly created text file. All I see is the last one "5 105"
I'm guessing this is because each time the detail line method runs, it writes over the last entry (or creates a new txt file over the last one). I thought I could get read of this problem by moving the creation point of my outputfile (summaryReport) to my main, but then my method has all sorts of problems..as it doesn't recognize what summaryReport is.
Can anyone point me in the right direction.
public class SpacelySprockets
{
public static void main(String[] args) //Main Program
{ //Start Main
int partNumber=0;
int quantity=0;
int part1Total=0;
int part2Total=0;
int part3Total=0;
int part4Total=0;
int part5Total=0;
Keyboard kbd;
kbd = new Keyboard ();
InputFile sprockets;
sprockets = new InputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\sprockets.txt");
OutputFile summaryReport;
summaryReport = new OutputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\summaryReport.txt");
while (!sprockets.eof())
{ //Start Loop
partNumber=sprockets.readInt ();
quantity=sprockets.readInt ();
switch (partNumber)
{
case 1: part1Total = part1Total + quantity;
break;
case 2: part2Total = part2Total + quantity;
break;
case 3: part3Total = part3Total + quantity;
break;
case 4: part4Total = part4Total + quantity;
break;
case 5: part5Total = part5Total + quantity;
break;
default: System.out.println ("The file has unknown part numbers ltstd");
}
} //End Loop
detailLine (part1Total, 1);
detailLine (part2Total, 2);
detailLine (part3Total, 3);
detailLine (part4Total, 4);
detailLine (part5Total, 5);
} //End Main
public static void detailLine (int partTotal, int partNumber)
{
OutputFile summaryReport;
summaryReport = new OutputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\summaryReport.txt");
summaryReport.writeString (partNumber+" "+partTotal);
summaryReport.writeEOL();
summaryReport.close();
}
} //End Class