Results 1 to 3 of 3
- 07-21-2012, 01:23 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
help with formatting output while reading a file
Hi guys,
I am trying to read a text file and format the output a certain way. This is what I have so far and all it does it outputs the entire text file, I can't figure out how to have it print line by line, do the calculation and format the text, I looked at formatter but it would just print everything on a single line... any input would be greatly appreciated!
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
public class ScannerReadFile {
public static void main(String[] args) {
String name, major;
File file = new File("C:\\Users\\pericjo\\workspace\\john\\src\\da ta.txt");
try {
Scanner scn = new Scanner(file);
while (scn.hasNextLine()) {
String line = scn.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
}
}
}
Here's the text file content:
John Smith
CS
89
99
Jonathan Sou
MATH
78
98
College
CS
78
87
Math Dept
MATH
99
66
Comp Science
CS
88
100
And the output should look like:
John Smith, CS, 89.0, 99.0
Jonathan Sou, MATH, 78.0, 98.0
College, CS, 78.0, 87.0
Math Dept, MATH, 99.0, 66.0
Comp Science, CS, 88.0, 100.0
----- ---------- ----------
MATH Math AVG= 88.5 Java AVG= 82.0
CS Math AVG= 85.0 Java AVG= 95.33333333333333
Thanks!
- 07-21-2012, 04:03 AM #2
Re: help with formatting output while reading a file
Why do they call it rush hour when nothing moves? - Robin Williams
- 07-23-2012, 10:48 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Formatting Output
By Rahim2312 in forum New To JavaReplies: 18Last Post: 05-04-2012, 09:45 AM -
formatting data output
By droidus in forum New To JavaReplies: 16Last Post: 04-12-2011, 11:26 PM -
Help with formatting output
By John Lord in forum New To JavaReplies: 1Last Post: 10-31-2010, 12:10 PM -
Need help formatting File Output
By aaroncarpet in forum New To JavaReplies: 2Last Post: 11-26-2009, 05:26 PM -
Need help formatting output and some code
By A5i19 in forum New To JavaReplies: 6Last Post: 11-09-2009, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks