Sunday, 11 August 2013

Reading a text file of student names and text scores and finding an average

Reading a text file of student names and text scores and finding an average

Currently I have text file that stores student names and their test
scores. The format of the file is last name, first name, and test score.
Each value in the text file is separated but a space. So the file looks
similar to this:
Smith John 85 Swan Emma 75
I've got the code running so that it prints all the data to the console,
but what I can't get it to do is take all the test scores, add them up,
and find the average and print the average to the console. As well as
print any students' whose score is 10 less then the average.
Right now this is the code I'm using to read and print the information to
the console.
public class ReadTXT
{
public static void main(String[] args)
{
{
String txtFile = "/Users/Amanda/Desktop/Studentdata.txt";
BufferedReader br = null;
String line = "";
String txtSplitBy = " ";
try {
br = new BufferedReader(new FileReader(txtFile));
while ((line = br.readLine()) != null) {
String[] LastName= line.split(txtSplitBy);
String[] FirstName = line.split(txtSplitBy);
String[] TS = line.split(txtSplitBy);
System.out.println("Last Name: " + LastName[0]
+ "\n" +"First Name: " + FirstName[1] + "\n" +
"Test Score: " + TS [2] + "\n") ;
{
double average = sum / i;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
}
And this is the code I was trying to use to find the average but I keep
getting an exception.
int i =Integer.parseInt ("TS");
double sum = 0.0;
for (int x = 0; x < i; sum += i++);
{
double average = sum / i;
}
I am new to learning java but I am trying.

No comments:

Post a Comment