944,180 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1445
  • Java RSS
Apr 12th, 2006
0

Quick TT_EOL question

Expand Post »
Hello! Can anyone tell me how to make use of the End of Line constant (TT_EOL) when reading data from a file into a Vector? I understand that TT_EOL returns and int, while vectors only hold class types (Double in my case), so how can you compare the two? I would like my program to stop reading tokens into a vector when it reaches the end of the line in a text file. Here is my code, that produces an error stating that I cannot compare a String to an int:

Java Syntax (Toggle Plain Text)
  1. public void dataRetrieve(String name)
  2.  
  3. /**************************************************************************
  4. *Method Name: dataRetrieve
  5. *
  6. *Description: This method takes in the name variable and reads the file
  7. * line by line, parsing each test name (String) and each test
  8. * score (integer). For each line, it adjusts all applicable
  9. * variables as necessary and keeps track of total tests and
  10. * total scores by range (for each test). It will throw an IO
  11. * exception if there are problems reading from the file.
  12. * Reads file until no more lines.
  13. *
  14. *
  15. *Precondition: Text file exists.
  16. *
  17. *Postcondition: Multiple variable values are updated to reflect all file
  18. * input. Total tests are counted.
  19. *
  20. *Author: Melissa M. Cazalet
  21. ***************************************************************************/
  22.  
  23. {
  24. try
  25. {
  26. BufferedReader inputStream = new BufferedReader(new FileReader(name));
  27.  
  28. String trash = "No trash yet";
  29. testNames = new Vector<String>();
  30. while ((trash = inputStream.readLine()) !=null)
  31. {
  32. StringTokenizer st = new StringTokenizer(trash);
  33. testNames.addElement(st.nextToken());
  34.  
  35. lineCount++;
  36.  
  37. Vector<Double> tempScores;
  38. while(st.nextToken()!= StreamTokenizer.TT_EOL)
  39. {
  40. tempScores = new Vector<Double>();
  41. tempScores.addElement(new Double(st.nextToken());
  42. }
  43.  
  44. int index;
  45. double sum = 0;
  46. for(index = 0; index<tempScores.size(); index++)
  47. {
  48. sum = sum + tempScores.elementAt(index);
  49. }
  50. double average = sum/tempScores.size();
  51. testMeans.addElement(average);
  52.  
  53. }
  54.  
  55. inputStream.close();
  56. }
  57. catch(IOException e)
  58. {
  59. System.out.println("Problem reading from file.");
  60. }

Thanks for any/all help!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cazaletm is offline Offline
3 posts
since Mar 2006

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: On Click Event for an array of buttons
Next Thread in Java Forum Timeline: Multiple operations from 2 inputs.





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


Follow us on Twitter


© 2011 DaniWeb® LLC