• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data reading/output from file

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only use the tokens that are numbers and ignore the thrown exceptions for the rest of the tokens. It might be better to check first if the token is a number or not, something like this:

[/qb]<hr></blockquote>
[ May 27, 2004: Message edited by: Bart Sawyer ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I wondered how you'd skip over name to get the numbers. You didn't really skip, you just caught the number exceptions when the tokens were part of the name. Since those are exactly the bits of information that are missing from your output, think about what could go in the catch clause to show those tokens to the user. Maybe print them as you find them? Maybe build up a name string and print it later?
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic