Question

In: Computer Science

Complete the program to read in values from a text file. The values will be the...

Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number of assignments (number of columns), respectively. You may assume that all values in the file will be integers.

For example, if the file contains:

3
4
9 6 10 5
2 2 0 0
10 10 9 10

then it represents the scores for 3 students on 4 assignments as follows:

Assignment 1 Assignment 2 Assignment 3 Assignment 4
Student 1 9 6 10 5
Student 2 2 2 0 0
Student 3 10 10 9 10

The program should:

1) Read in the first two values, two integers, to get the number of students and number of assignments. Create a two-dimensional array of integers to store all the scores using these dimensions.

2) Read in all the scores into the 2-D array.

3) Print the whole array, row by row on separate lines, using Arrays.toString() on each row of the array

3) Print the average of the scores on each assignment, rounded to 1 decimal place. For example, if the data is given in the above example, the output would be:

Array of scores:
[9, 6, 10, 5]
[2, 2, 0, 0]
[10, 10, 9, 10]
Average score of each assignment:
Assignment #1 Average = 7.0
Assignment #2 Average = 6.0
Assignment #3 Average = 6.3
Assignment #4 Average = 5.0

This is what I have so far:

import java.util.*;
import java.io.File;
import java.io.*;

public class Scores {
public static void main(String[] args) throws FileNotFoundException {
Scanner scnr = new Scanner(System.in);
System.out.println("What is the name of the file containing the scores?");
String file = scnr.nextLine();
Scanner fileScan = new Scanner(new File(file));
int rows = Integer.parseInt(fileScan.nextLine());
int col = Integer.parseInt(fileScan.nextLine());
int[][] scores = new int[rows][col];
int i = 0;
while (fileScan.hasNext()) {
String line = fileScan.nextLine();
String[] strArray = line.split(" ");
for (int c = 0; c < strArray.length; c++) {
scores[i][c] = Integer.parseInt(strArray[c]);
}
i++;
}
System.out.println("Array of scores:");
for (int c = 0; c < rows; c++) {
for (int j = 0; j < col; j++) {
System.out.print(scores[c][j] + " ");
}
System.out.println();
}
System.out.println("Average score of each assignment:");

for (int c = 0; c < col; c++) {
int sum = 0;
for (int j = 0; j < rows; j++) {
sum = sum + scores[j][c];
}
System.out.println("Assignment #" + (c + 1) + " Average = " + (sum / 3));
}

}
}

But I keep getting errors similar to this:

What is the name of the file containing the scores?
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Scores.main(Scores.java:12)

Solutions

Expert Solution

PROGRAM CODE

import java.text.DecimalFormat;
import java.util.*;
import java.io.File;
import java.io.*;

public class Scores {

public static void main(String[] args) throws FileNotFoundException {

Scanner scnr = new Scanner(System.in);

System.out.println("What is the name of the file containing the scores?");
String file = scnr.nextLine();
Scanner fileScan = new Scanner(new File(file));

// Reading number of rows (number of students) and columns (number of assignments)
// Just use fileScan.nextInt() to get int instead of using Integer.parseInt(fileScan.nextLine())

int rows = fileScan.nextInt();
int col = fileScan.nextInt();

int[][] scores = new int[rows][col]; // Declaring 2D array to store scores

// Using nested-for loop to read all assignment marks into 2D array

for (int i=0; i<rows; i++) {

for (int j=0; j<col; j++) {

scores[i][j] = fileScan.nextInt();
}
}

System.out.println("Array of scores:");

for (int c = 0; c < rows; c++) {

// Printing each row using Arrays.toString

System.out.println(Arrays.toString(scores[c]));
}

System.out.println("Average score of each assignment:");

for (int c = 0; c < col; c++) {
int sum = 0;
for (int j = 0; j < rows; j++) {
sum = sum + scores[j][c];
}

// Declaring variable for average

double avg = sum / 3.0;

System.out.println("Assignment #" + (c + 1) + " Average = " + new DecimalFormat("0.0").format(avg)); // formatting to 1 decimal place
}

}
}

score.txt

3
4
9 6 10 5
2 2 0 0
10 10 9 10

SAMPLE OUTPUT

---------------------------------------------------------------------------------------------------------

COMMENT DOWN FOR ANY QUESTIONS...........
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT