currently my program only reads in the one first name and last name with 5 grades and computes the average from the first line of my input file.
eg.
Albert Lake 54 23 54 32 12 average test score 35

im trying to edit this code so that the file reads in multiple first and last names each having 5 grades and does the average just like its doing. im sure its possible to read in multple names on new lines in a text file without introducing more variables, im just not sure how to accomplish this.

example of input file...
Albert Lake 54 23 54 32 12
Paul Brown 32 43 65 87 34
sam jones 32 45 65 76 87
....

please give me some ideas on how i can do this.
Ty


double test1, test2, test3, test4, test5;
double average;
String firstName;
String lastName;
StringTokenizer tokenizer;

BufferedReader inFile = new
BufferedReader (new FileReader ("a:\\test.txt"));

PrintWriter outFile = new
PrintWriter(new FileWriter ("a:testavg.txt"));

DecimalFormat twoDecimal = new DecimalFormat ("0.00");

tokenizer =
new StringTokenizer (inFile.readLine());
firstName = tokenizer.nextToken();
lastName = tokenizer.nextToken();
outFile.println ("Student Name: " + firstName + " " + lastName);

test1 = Double.parseDouble(tokenizer.nextToken());
test2 = Double.parseDouble(tokenizer.nextToken());
test3 = Double.parseDouble(tokenizer.nextToken());
test4 = Double.parseDouble(tokenizer.nextToken());
test5 = Double.parseDouble(tokenizer.nextToken());

outFile.println("Test scores: "
+ twoDecimal.format (test1) + " "
+ twoDecimal.format (test2) + " "
+ twoDecimal.format (test3) + " "
+ twoDecimal.format (test4) + " "
+ twoDecimal.format (test5));

average = (test1 + test2 + test3 + test4 + test5) /5.0;

outFile.println ("Average test score: "
+ twoDecimal.format(average));

outFile.close();
}
}

Recommended Answers

All 6 Replies

Try using arrays instead of single variables, and just throw the reading code into a loop.

thank you very much for responding narrue but im still very much puzzled as to going about implementing an array into this. I just keep gettin stuck on how to create a loop on the readline section of the code so it reads the file line by line and outputs all these lines to a next file.
maybe its late and im not thinking straight :)...but i won't be able to sleep until i get this

>I just keep gettin stuck on how to create a loop on the readline section of the code
Try something like this:

String line;

while ((line = inFile.readLine()) != null) {
  // Tokenize and process
}

As for the array part, it would be considerably easier to treat each record as a complete entity rather than separate variables:

class Record {
  private double[] tests;
  private double average;
  private String firstName;
  private String lastName;

  public Record(String first, String last, double[] tests);
  public display();
};

Then you can create a data structure (such as an array) of Records. However, if you don't need to save a record before processing another, there's really no need to go to that kind of effort. You can just use the stream reading loop above with the code you have.

here is the adjustments i have made to my program. I was told by my professor that it was not necessary to use an array to read in several new lines and and send the data back to an output file. this code compiles but its shows a run time error.

// java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at StudentGrade.main(StudentGrade.java:35)
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

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

public class StudentGrade
{
  public static void main (String[] args) throws IOException, FileNotFoundException

  {

    //Declare Variables

    double test1, test2, test3, test4, test5;
    double average;
    String firstName;
    String lastName;
    StringTokenizer tokenizer;
    int count = 0;
    String inputLine = null;
    BufferedReader inFile = new
      BufferedReader (new FileReader ("a:\\test.txt"));

    PrintWriter outFile = new
      PrintWriter(new FileWriter ("a:testavg.txt"));

    DecimalFormat twoDecimal = new DecimalFormat ("0.00");



    inputLine = inFile.readLine();                         
    while(inputLine != null)
    {    
      tokenizer = new StringTokenizer(inputLine);

      firstName = tokenizer.nextToken();  
      lastName = tokenizer.nextToken();
      outFile.println ("Student Name: " + firstName + " " + lastName);

      test1 = Double.parseDouble(tokenizer.nextToken());
      test2 = Double.parseDouble(tokenizer.nextToken());
      test3 = Double.parseDouble(tokenizer.nextToken());
      test4 = Double.parseDouble(tokenizer.nextToken());
      test5 = Double.parseDouble(tokenizer.nextToken());

      outFile.println("Test scores: "
        + twoDecimal.format (test1) + " "
        + twoDecimal.format (test2) + " "
        + twoDecimal.format (test3) + " "
        + twoDecimal.format (test4) + " "
        + twoDecimal.format (test5));

      average = (test1 + test2 + test3 + test4 + test5) /5.0;

      outFile.println ("Average test score: "
        + twoDecimal.format(average));

      inputLine = inFile.readLine(); 
    }	
    outFile.close();
  }
}

Code tags added. -Narue

My first guess would be a formatting issue in your input file.

Ty Narue....your first guess was pretty much correct. I had skipped a line in between the data on the input file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.