I need help on this one.... Write a program that takes as input student scores f
ID: 3640325 • 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.*;
import java.util.*;
public class Student {
private java.util.ArrayList studentId;
private java.util.ArrayList avgScore;
Student() {
studentId = new ArrayList();
avgScore = new ArrayList();
}
public void readFile(String fileName) {
try {
// Create FileReader Object
FileInputStream inputFileReader = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(inputFileReader);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(in));
String inLine = null;
while ((inLine = inputStream.readLine()) != null) {
StringTokenizer token = new StringTokenizer(inLine, " ");
int index = 0;
double score = 0.0, total = 0.0;
while (token.hasMoreTokens()) {
if (index == 0) {
studentId.add(token.nextToken());
token.nextToken();
index++;
} else {
score = score + Integer.parseInt(token.nextToken());
total++;
}
}
avgScore.add(score / total);
}
inputStream.close();
in.close();
} catch (IOException e) {
System.out.println("IO Exception : " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception : " + e.getMessage());
} finally {
}
}
public void writeFile(String fileName) {
try {
FileWriter fstream = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(fstream);
for (int i = 0; i < studentId.size(); i++) {
out.write(studentId.get(i) + " - " + avgScore.get(i));
out.newLine();
}
out.close();
} catch (Exception e) {
System.out.println("StoreData Exception : " + e.getMessage());
}
}
public static void main(String[] args) {
Student stud = new Student();
stud.readFile("studentScore.dat");
stud.writeFile("studentAvgScore.dat");
}
}
import java.io.*;
import java.util.*;
public class Student {
private java.util.ArrayList studentId;
private java.util.ArrayList avgScore;
Student() {
studentId = new ArrayList();
avgScore = new ArrayList();
}
public void readFile(String fileName) {
try {
// Create FileReader Object
FileInputStream inputFileReader = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(inputFileReader);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(in));
String inLine = null;
while ((inLine = inputStream.readLine()) != null) {
StringTokenizer token = new StringTokenizer(inLine, " ");
int index = 0;
double score = 0.0, total = 0.0;
while (token.hasMoreTokens()) {
if (index == 0) {
studentId.add(token.nextToken());
token.nextToken();
index++;
} else {
score = score + Integer.parseInt(token.nextToken());
total++;
}
}
avgScore.add(score / total);
}
inputStream.close();
in.close();
} catch (IOException e) {
System.out.println("IO Exception : " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception : " + e.getMessage());
} finally {
}
}
public void writeFile(String fileName) {
try {
FileWriter fstream = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(fstream);
for (int i = 0; i < studentId.size(); i++) {
out.write(studentId.get(i) + " - " + avgScore.get(i));
out.newLine();
}
out.close();
} catch (Exception e) {
System.out.println("StoreData Exception : " + e.getMessage());
}
}
public static void main(String[] args) {
Student stud = new Student();
stud.readFile("studentScore.dat");
stud.writeFile("studentAvgScore.dat");
}
}
Related 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.