Hi!
I have a set of data, such as this:
Anne Other 15 19 15 12 18
Roger the Dodger 11 13 9 15
Eric the Red 19 19 20 15 20
Java Janet 20 20 20
What I need to do is to create another file and print the sum of this data, so it looks like this:
Anne Other 79
Roger the Dodger 48
Eric the Red 93
Java Janet 60
My window however shows the following output:
79
48
93
60
How do I get Java to print out the names from the other file? Do I need to store all of the data in an array?
Here is my code:
public class ProcessingMarks {
public static void main (
String [] args) throws IOException {
/*BufferedReader br =
new BufferedReader(new FileReader("marks.txt"));
FileWriter fw = new FileWriter(fileName, true);
PrintWriter pw = new PrintWriter(fw);*/
FileReader fr = new FileReader("marks.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("results.txt");
PrintWriter pw = new PrintWriter(fw);
String line = br.readLine();
int sum;
while (line != null) {
StringTokenizer st = new StringTokenizer(line);
sum = 0;
while (st.hasMoreTokens()) {
try {
//sum = sum + Integer.valueOf(st.nextToken());
//sum = sum + Integer.valueOf(st.nextToken()).intValue();
sum = sum + Integer.parseInt(st.nextToken());
} catch (NumberFormatException e) {}
}
pw.println("\t"+sum);
//pw.println(line+"\t"+sum);
line = br.readLine();
}
br.close();
//pw.println(line+"\t"+sum);
pw.close();
}
}
Looking forward to your response!