JAVA Question
a)
Read a file named testScoreIn.txt representing test scores for a
class of students.
Each record contains an id number and three test scores (Note: the
test scores are integers).
For example, a student record might look like:
BK1234 87 72 91
b)
Without using an array, read each student’s information, one record
at a time and compute the student’s average (as a double).
c)
Write to an output file, testScoreOut.txt, as a neatly formatted
table with column headings, each student's id, three grades and
average grade (as a real number to 3 decimal places)
d)
At the end, when the the sentinel ID, "LastID", is read, print to
the screen the id number of the student with the highest
average.
Code:-
Main.java
import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
class Main{
public static void main (String[] args) throws Exception,IOException{
//for reading each line from textScoreIn.txt
File file_in = new File("textScoreIn.txt");
Scanner sc = new Scanner(file_in);
FileWriter fw = new FileWriter("textScoreOut.txt");
String id_1="";
Double avg=Double.valueOf(0);
while (sc.hasNextLine()){
String id=sc.next();
int f = sc.nextInt();
int s = sc.nextInt();
int t = sc.nextInt();
Double average =(Double.valueOf(f)+Double.valueOf(s)+Double.valueOf(t))/3;
if(average>avg){ //To get the maximum average and id
id_1=id;
avg=average;
}
String p=id+" "+String.valueOf(f)+" "+String.valueOf(s)+" "+String.valueOf(t)+" "+Double.toString(average)+"\n";
fw.write(p);
}
sc.close();
fw.close();
System.out.println("The Student ID having the highest average is");
System.out.println(id_1);
}
}
Output:-
Screenshot of Code:-
textScoreIn.txt
if u like my answer then please give a thumbs up..!!
Get Answers For Free
Most questions answered within 1 hours.