/* Craig Persiko
CS 111A GradeInput sample program
Outputs student and grade data to a file
in formatted report format.
*/
import java.io.*;
import java.util.Scanner;
public class GradeInput
{
public static void main(String args[]) throws IOException
{
Scanner scan = new Scanner(System.in);
String name;
double grade;
PrintWriter fileOut = new PrintWriter("grades.txt");
fileOut.println("Name Grade");
fileOut.println("---------------------------------");
do
{
System.out.println("Please enter a student's name, or hit Enter to exit");
name = scan.nextLine();
if(name.equals(""))
break;
System.out.println("Please enter the test grade: ");
grade = scan.nextDouble();
scan.nextLine();
// output to the file:
// the name left-justified, in a field of 20 characters
// followed by the grade right-justified in a field of 12 characters,
// with 2 digits after the decimal point
fileOut.printf("%-20s %12.2f\n", name, grade);
}while(name.length() != 0); // we really break out of the loop above. Zero length = ""
System.out.println("Grade report saved to grades.txt");
fileOut.close();
}
}
/* Sample Output:
-bash-3.2$ java GradeInput
Please enter a student's name, or hit Enter to exit
Craig Persiko
Please enter the test grade:
95.6666
Please enter a student's name, or hit Enter to exit
Lin Chan
Please enter the test grade:
95
Please enter a student's name, or hit Enter to exit
Elena Sanchez
Please enter the test grade:
100.5
Please enter a student's name, or hit Enter to exit
John Smith
Please enter the test grade:
1.9999
Please enter a student's name, or hit Enter to exit
Grade report saved to grades.txt
-bash-3.2$ cat grades.txt
Name Grade
---------------------------------
Craig Persiko 95.67
Lin Chan 95.00
Elena Sanchez 100.50
John Smith 2.00
-bash-3.2$
*/
syntax highlighted by Code2HTML, v. 0.9