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

Reply

Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

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

 
0
  #1
Mar 6th, 2005
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();
}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Mar 6th, 2005
Try using arrays instead of single variables, and just throw the reading code into a loop.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

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

 
0
  #3
Mar 7th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Mar 7th, 2005
>I just keep gettin stuck on how to create a loop on the readline section of the code
Try something like this:
  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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

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

 
0
  #5
Mar 7th, 2005
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #6
Mar 7th, 2005
My first guess would be a formatting issue in your input file.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

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

 
0
  #7
Mar 7th, 2005
Ty Narue....your first guess was pretty much correct. I had skipped a line in between the data on the input file
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC