| | |
help on using StringTokenizer to read and compute multiple lines from text
![]() |
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
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: 26
Reputation:
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:
Java Syntax (Toggle Plain Text)
String line; while ((line = inFile.readLine()) != null) { // Tokenize and process }
Java Syntax (Toggle Plain Text)
class Record { private double[] tests; private double average; private String firstName; private String lastName; public Record(String first, String last, double[] tests); public display(); };
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
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.
Java Syntax (Toggle Plain Text)
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(); } }
![]() |
Similar Threads
- Counting lines in a text file and further operations (C++)
- read-delete lines from a text file (PHP)
- print text in multiple lines in picturebox in vb6 (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: help with arrays, methods
- Next Thread: main class could not be found?
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






