943,983 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 11776
  • Java RSS
Mar 6th, 2005
0

help on using StringTokenizer to read and compute multiple lines from text

Expand Post »
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();
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 6th, 2005
0

Re: help on using StringTokenizer to read and compute multiple lines from text

Try using arrays instead of single variables, and just throw the reading code into a loop.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: help on using StringTokenizer to read and compute multiple lines from text

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
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 7th, 2005
0

Re: help on using StringTokenizer to read and compute multiple lines from text

>I just keep gettin stuck on how to create a loop on the readline section of the code
Try something like this:
Java Syntax (Toggle Plain Text)
  1. String line;
  2.  
  3. while ((line = inFile.readLine()) != null) {
  4. // Tokenize and process
  5. }
As for the array part, it would be considerably easier to treat each record as a complete entity rather than separate variables:
Java Syntax (Toggle Plain Text)
  1. class Record {
  2. private double[] tests;
  3. private double average;
  4. private String firstName;
  5. private String lastName;
  6.  
  7. public Record(String first, String last, double[] tests);
  8. public display();
  9. };
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: help on using StringTokenizer to read and compute multiple lines from text

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.

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4.  
  5. public class StudentGrade
  6. {
  7. public static void main (String[] args) throws IOException, FileNotFoundException
  8.  
  9. {
  10.  
  11. //Declare Variables
  12.  
  13. double test1, test2, test3, test4, test5;
  14. double average;
  15. String firstName;
  16. String lastName;
  17. StringTokenizer tokenizer;
  18. int count = 0;
  19. String inputLine = null;
  20. BufferedReader inFile = new
  21. BufferedReader (new FileReader ("a:\\test.txt"));
  22.  
  23. PrintWriter outFile = new
  24. PrintWriter(new FileWriter ("a:testavg.txt"));
  25.  
  26. DecimalFormat twoDecimal = new DecimalFormat ("0.00");
  27.  
  28.  
  29.  
  30. inputLine = inFile.readLine();
  31. while(inputLine != null)
  32. {
  33. tokenizer = new StringTokenizer(inputLine);
  34.  
  35. firstName = tokenizer.nextToken();
  36. lastName = tokenizer.nextToken();
  37. outFile.println ("Student Name: " + firstName + " " + lastName);
  38.  
  39. test1 = Double.parseDouble(tokenizer.nextToken());
  40. test2 = Double.parseDouble(tokenizer.nextToken());
  41. test3 = Double.parseDouble(tokenizer.nextToken());
  42. test4 = Double.parseDouble(tokenizer.nextToken());
  43. test5 = Double.parseDouble(tokenizer.nextToken());
  44.  
  45. outFile.println("Test scores: "
  46. + twoDecimal.format (test1) + " "
  47. + twoDecimal.format (test2) + " "
  48. + twoDecimal.format (test3) + " "
  49. + twoDecimal.format (test4) + " "
  50. + twoDecimal.format (test5));
  51.  
  52. average = (test1 + test2 + test3 + test4 + test5) /5.0;
  53.  
  54. outFile.println ("Average test score: "
  55. + twoDecimal.format(average));
  56.  
  57. inputLine = inFile.readLine();
  58. }
  59. outFile.close();
  60. }
  61. }
Code tags added. -Narue
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 7th, 2005
0

Re: help on using StringTokenizer to read and compute multiple lines from text

My first guess would be a formatting issue in your input file.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: help on using StringTokenizer to read and compute multiple lines from text

Ty Narue....your first guess was pretty much correct. I had skipped a line in between the data on the input file
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help with arrays, methods
Next Thread in Java Forum Timeline: Setting Font of Button Label





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC