Please Help me if you can. I'm doing a project that is supposed to output Averages on student Test Scores (5 Students), and output their names, as well as display the highest score. The student score are read through an excel .csv file.
Here is the two files that I have come up with this far.
TestscoreReader.java
package assignment; //**************************************************** //The TestScoreReader class reads test scores as * //tokens from a file and calculates the average * //of each line of scores. * //**************************************************** import java.io.*; //import java.util.StringTokenizer; import java.util.Scanner; public class TestScoreReader { private Scanner inputFile; private String line; //************************************************* // The constructor opens a file to read the grades* // from * //************************************************* public TestScoreReader(String filename)throws IOException { File file = new File(filename); inputFile = new Scanner(file); } //************************************************** // The readNextLine method reads the next line * // from the file. If a line was read, the method * // returns true. Otherwise it returns false. * //************************************************** public boolean readNextLine() throws IOException { boolean lineRead; // Flag indicating successful read // Determine whether there is more to read. lineRead = inputFile.hasNext(); // if so, read the next line. if (lineRead) line = inputFile.nextLine(); // Return the status of the read operation. return lineRead; } //************************************************** // The getAverage method returns the average of * // last set of test scores read from the file. * //************************************************** public double getAverage() { int total = 0; // Accumulator double average; // To hold the average test score //Tokenize the last line read from the file. String[] tokens = line.split(","); // Calculate the total of the test scores for (String str : tokens) { total += Integer.parseInt(str); } // Calculate the average of the test scores. // Use a cast to avoid integer division. average = (double) total / tokens.length; // Return the average. return average; } //************************************************** // The close method closes the file. * //************************************************** public void close() throws IOException { inputFile.close(); } }
and...
Testaverages.java
package assignment; //******************************************************** //This program uses the TestScoreReader class to read * //test scores from a file and get their averages. * //******************************************************** import java.io.*; public class TestAverages { public static void main(String[] args) throws IOException { double average; // To hold an average int studentNumber = 1; // To count students // Create a TestScoreReader object. TestScoreReader scoreReader = new TestScoreReader("/Users/cameroncashwell/Documents/Grades.csv"); // Process the file contents. while (scoreReader.readNextLine()) { // Get this student's average. average = scoreReader.getAverage(); // Display this student's average. System.out.println("Average for student number " + studentNumber + " is " + average); // Increment the student number. studentNumber++; } // Close the file. scoreReader.close(); System.out.println("No more scores."); } }
Here is my compiled errors.
"Exception in thread "main" java.lang.NumberFormatException: For input string: "Jane Doe"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at assignment.TestScoreReader.getAverage(TestScoreRea der.java:68)
at assignment.TestAverages.main(TestAverages.java:26) "
So I think I'm understanding how to display the average. Still trying to figure out how to display highest score.. And trying to figure out why it's getting an error with the value of the .csv file.
Not asking anyone to do this for me, just a point toward tutorials, advice, something to work with or any help you can give. Not too great at Programming so will probably be up all night with this thing.
Thanks in advance.