Quick TT_EOL question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 3
Reputation: cazaletm is an unknown quantity at this point 
Solved Threads: 0
cazaletm cazaletm is offline Offline
Newbie Poster

Quick TT_EOL question

 
0
  #1
Apr 12th, 2006
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:

  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!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC