•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,768 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,478 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5873 | Replies: 6
![]() |
•
•
Join Date: Oct 2004
Posts: 16
Reputation:
Rep Power: 4
Solved Threads: 0
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();
}
}
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();
}
}
•
•
Join Date: Oct 2004
Posts: 16
Reputation:
Rep Power: 4
Solved Threads: 0
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
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:
As for the array part, it would be considerably easier to treat each record as a complete entity rather than separate variables:
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.
Try something like this:
String line;
while ((line = inFile.readLine()) != null) {
// Tokenize and process
}class Record {
private double[] tests;
private double average;
private String firstName;
private String lastName;
public Record(String first, String last, double[] tests);
public display();
}; Member of: Beautiful Code Club.
•
•
Join Date: Oct 2004
Posts: 16
Reputation:
Rep Power: 4
Solved Threads: 0
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.
Code tags added. -Narue
// 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();
}
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- print text in multiple lines in picturebox in vb6 (Visual Basic 4 / 5 / 6)
- Searching for text in huge (> 5GB) text files. (C++)
- StringTokenizer??? NEED HELP PLEASE!!! (Java)
Other Threads in the Java Forum
- Previous Thread: help with arrays, methods
- Next Thread: main class could not be found?



Linear Mode