Averages, Highest & using a .csv file ! ? ! ?
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.:confused::((
Here is the two files that I have come up with this far.
TestscoreReader.java
Code :
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
Code :
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.:confused:
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.:eek:
Thanks in advance.:cool:
Re: Averages, Highest & using a .csv file ! ? ! ?
Hello thebigtimeGNAR.
Welcome to the Java Programming Forums :)
Can you please attach your .csv file? I will then attempt to compile your code...
Re: Averages, Highest & using a .csv file ! ? ! ?
It's telling me the file is Invalid to upload?
but it consists of a simple spreadsheet similar to,
"Jane Doe, 86, 88, 77, 92, 99"
"Another Person, 88, 33, 99, 100"
and 3 others similar.
So, I'm understanding how to pull the numeric values from the .csv file, but am unable to figure out a way to pull the names to output with the averages. I'm trying to use getName & char but not figuring out how to finish through with them, declare them etc. Am I going the right route?[-O<
Re: Averages, Highest & using a .csv file ! ? ! ?
Also, I'm supposed to output 'the highest score' as well. So i'm thinking throwing something like this in there-
Code :
public String getName(){ 079 String name = file.getName(); 080 if (ref != null && !"".equals(ref)){ 081 name += "#" + ref;