I am asked to print out a 2d array given a text file students.txt. The text file
ID: 3806890 • Letter: I
Question
I am asked to print out a 2d array given a text file students.txt. The text file has names of students and test grades. The program is asked to split each and put onyl the grades into a 2d array. The following is what I have written so far. activity 1 and 2 are needed but i can manage two if I can split the lines of each file into two strings and use them in number 2 later
import java.io.*;
import java.util.*;
import java.util.Arrays;
public class myIO {
/****************************************************************************
* Activity 2:
* This method takes the name of a file as input.
* It reads a file formatted like students.txt (provided to you):
* This file contains several lines: each line starts with the name of
* a student (last followed by first)
* and continues with a list of their grades
* Each line contains the same number of grades
* You have to create one array from it:
* an array of integer grades (2D array: n lines and p columns, p being the number of grades per student)
****************************************************************************/
public static int[][] readGradesFromFile(String fileName) throws FileNotFoundException, IOException {
int[][] grades = new int [5][4] ;
FileReader fr = new FileReader("students.txt");
BufferedReader textReader = new BufferedReader(fr);
String line = textReader.readLine();
String[] grade = line.split(" ");
int[] someInt = Integer.parseInt(grade[]);
for(int i = 0; i < grades.length ; i++) {
for(int j = 2; j <grades[i].length ; i++) {
grades[i][j] = Integer.parseInt(grade[j]);
System.out.print(grades[i][j]);
}
}
return grades;
}
/****************************************************************************
* Activity 2:
* This method takes a 2D array A and the name of a file, filename, as a parameter
* and creates a new file, filename.average
* filename.average contains at each line:
* the name of the student written in filename at the same line
* the average of the grades of this student, as stored in the 2D array A
******************************************************************************/
// public static void writeAveragesToFile(String filename, int[][] A) {
// your code goes here
// }
/****************************************************************************
* Main method in which the above methods are tested
****************************************************************************/
public static void main(String[] args) throws FileNotFoundException, IOException {
int[][] GradesArr = readGradesFromFile("students.txt");
}
}
this is the file contents
Diner Dylan 34 76 99 100
Basillas Bertha 98 96 78 89
Aranca Alberto 74 90 99 45
Estacion Esther 87 90 76 101
Castillo Carlos 76 45 67 86.
I
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.io.*;
public class myIO {
/****************************************************************************
* Activity 2:
* This method takes the name of a file as input.
* It reads a file formatted like students.txt (provided to you):
* This file contains several lines: each line starts with the name of
* a student (last followed by first)
* and continues with a list of their grades
* Each line contains the same number of grades
* You have to create one array from it:
* an array of integer grades (2D array: n lines and p columns, p being the number of grades per student)
****************************************************************************/
public static int[][] readGradesFromFile(String fileName) throws FileNotFoundException, IOException {
int[][] grades = new int [5][4] ;
FileReader fr = new FileReader("students.txt");
BufferedReader textReader = new BufferedReader(fr);
//int[] someInt = Integer.parseInt(grade[]);
for(int i = 0; i < grades.length ; i++) {
String line = textReader.readLine();
String[] grade = line.split(" ");
//System.out.println(line);
int k=2;
for(int j = 0; j <grades[i].length ; j++) {
//System.out.print(grades[i][j]+" "+grade[k]);
grades[i][j] = Integer.parseInt(grade[k++]);
}
}
textReader.close();
fr.close();
return grades;
}
/****************************************************************************
* Activity 2:
* This method takes a 2D array A and the name of a file, filename, as a parameter
* and creates a new file, filename.average
* filename.average contains at each line:
* the name of the student written in filename at the same line
* the average of the grades of this student, as stored in the 2D array A
* @throws IOException
******************************************************************************/
public static void writeAveragesToFile(String filename, int[][] A) throws IOException {
// your code goes here
int[][] grades = readGradesFromFile(filename);
FileWriter fw = new FileWriter(filename+".average");
BufferedWriter writer = new BufferedWriter(fw);
FileReader fr = new FileReader("students.txt");
BufferedReader textReader = new BufferedReader(fr);
for(int i = 0; i < grades.length ; i++) {
double sum = 0;
String line = textReader.readLine();
String[] grade = line.split(" ");
for(int j = 0; j <grades[i].length ; j++) {
sum = sum + grades[i][j];
}
writer.write(grade[0]+" "+grade[1]+" "+(sum/grades[i].length));
writer.newLine();
}
textReader.close();
fr.close();
writer.close();
fw.close();
}
/****************************************************************************
* Main method in which the above methods are tested
****************************************************************************/
public static void main(String[] args) throws FileNotFoundException, IOException {
int[][] grades = readGradesFromFile("students.txt");
writeAveragesToFile("students.txt", grades);
}
}
Output
########### students.txt.average #############
Diner Dylan 77.25
Basillas Bertha 90.25
Aranca Alberto 77.0
Estacion Esther 88.5
Castillo Carlos 68.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.