I need help on this one.... Write a program that takes as input student scores f
ID: 3638371 • Letter: I
Question
I need help on this one....Write a program that takes as input student scores from a file and outputs the average for each student to a file.
The input file will have the following format:
studentid1 score1 score2 score3 score4
studentid2 score1 score2 score3
The output file will have the following format:
studentid1 average_score
studentid2 average_score
So, if the input file has the following data:
0896 - 8 9 10 8
12345 - 9 9 9
8789 - 8 7 6
The output file will look like:
0896 - 8.75
12345 - 9
8789 - 7
* The input file name will be “studentScore.dat” and output file will be “studentAvgScore.dat”
* Student id will always be the first value in every line, it can begin with 0; if the student id begins with 0, the output file should display the student id as it appears in the input file (hint: what happens if nextInt is used to read the first token)
* Nested loop will be required, the outer loop will read each line and the inner loop will process tokens for each input line
Explanation / Answer
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.DecimalFormat; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Set; import java.util.StringTokenizer; public class StudentScore { /** * Method to read data from the specified file "studentScore.dat" * @param InputFileName - input filename * @return LinkedHashMap HashMap of stud id and Avg mark */ public LinkedHashMap readData(String InputFileName) { LinkedHashMap resMap = new LinkedHashMap(); try { /* * Sets up a file reader to read the file passed on the command line * one character at a time */ FileReader input = new FileReader(InputFileName); /* * Filter FileReader through a Buffered read to read a line at a * time */ BufferedReader bufRead = new BufferedReader(input); String line; // String that holds current file line int count = 0; // Line number of count // Read first line line = bufRead.readLine(); count++; // Read through file one line at time. Print line # and line while (line != null) { StringTokenizer stringTokenizer = new StringTokenizer(line, "-"); String key = stringTokenizer.nextToken(); String value = stringTokenizer.nextToken(); StringTokenizer st = new StringTokenizer(value, " "); Float sum = 0.0F; int cnt = 0; while (st.hasMoreTokens()) { String temp = st.nextToken(); Float tempFloat = Float.parseFloat(temp); sum = sum + tempFloat; cnt++; } Float avg = sum / cnt; //Rounding to 2 digits after decimal DecimalFormat df = new DecimalFormat("#.##"); String StrAvg = df.format(avg); resMap.put(key, StrAvg); line = bufRead.readLine(); count++; } bufRead.close(); }catch (IOException e) { // If another exception is generated, print a stack trace e.printStackTrace(); } return resMap; } /** * Method to write data into the specified file "studentAvgScore.dat" * @param LinkedHashMap HashMap of stud id and Avg mark */ public void writeData(LinkedHashMap map) { FileWriter fstream; try { fstream = new FileWriter("C:\studentAvgScore.dat"); BufferedWriter out = new BufferedWriter(fstream); Set set = map.keySet(); Iterator it = set.iterator(); while (it.hasNext()) { String key = (String) it.next(); System.out.println(key + " - " + map.get(key)); out.write(key + "- " + map.get(key)); out.newLine(); } out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String args[]) { String fileName = "C:\studentScore.dat"; StudentScore studentScore = new StudentScore(); LinkedHashMap hashMap = studentScore.readData(fileName); studentScore.writeData(hashMap); } } sample input(studentScore.dat) 0134 - 4 5 8 9 12345 - 8 6 54 11245 - 7 88 5 45625 - 1 78456 - 8 99 99 99 sample output(studentAvgScore.dat) 0134 - 6.5 12345 - 22.67 11245 - 33.33 45625 - 1 78456 - 76.25Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.