943,716 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 9438
  • Java RSS
Feb 15th, 2009
0

Reading from a .txt file to an ArrayList

Expand Post »
What I am trying to do is read from a file to an ArrayList and then display it using a JOptionPane. The only problem is the code i am having trouble with is in an ActionListener and I believe somehow I have created an infinite while loop because when I click the button the ActionListener is attached to, noting happens.
The .txt file I am trying to read from:
Java Syntax (Toggle Plain Text)
  1. Mary Anne
  2. A
  3. Joe Wright
  4. B
  5. Andrew Heath
  6. A

and the troublesome code segment:
Java Syntax (Toggle Plain Text)
  1. public class ViewClassesListener implements ActionListener
  2. {
  3. public void actionPerformed (ActionEvent event)
  4. {
  5. //entering the class and grade information into arrays
  6. try
  7. {
  8. FileInputStream APStream=new FileInputStream("CompSciAP.txt");
  9. DataInputStream APDIS = new DataInputStream(APStream);
  10. BufferedReader APBR=new BufferedReader(new InputStreamReader(APDIS));
  11.  
  12. String temp="";
  13.  
  14. while ((temp=APBR.readLine()) != null)
  15. {
  16. //infinite loop?
  17. CompSciAP.add(temp);
  18. }
  19. APStream.close();
  20. }
  21. catch (Exception e)
  22. {
  23. System.err.println("Error: " + e.getMessage());
  24. }
  25.  
  26. //displaying the information
  27. stop=CompSciAP.size();
  28. index=0;
  29. while (index<=stop)
  30. {
  31. //or is this the infinite loop?
  32. APLabel.setText(APLabel.getText()+CompSciAP.get(index)+"\n");
  33. index++;
  34. }
  35. CompSciAPPane.showMessageDialog(All, APLabel, "Computer Science AP", JOptionPane.PLAIN_MESSAGE);
  36. }
  37. }

Please help me!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
BrianK123 is offline Offline
25 posts
since Feb 2009
Feb 15th, 2009
0

Re: Reading from a .txt file to an ArrayList

I think I solved it. I changed the
Java Syntax (Toggle Plain Text)
  1. stop=CompSciAP.size();
  2. index=0;
  3. while (index<=stop)
to
Java Syntax (Toggle Plain Text)
  1. stop=CompSciAP.size();
  2. index=0;
  3. while (index<stop)

That should be it. I spent hours trying to solve the problem and all it was was a little = sign!
Reputation Points: 10
Solved Threads: 1
Light Poster
BrianK123 is offline Offline
25 posts
since Feb 2009
Feb 15th, 2009
0

Re: Reading from a .txt file to an ArrayList

nice work, althought I wouldn't think that would cause a infinite, more likely an ArrayIndexOutOfBounds... anyway, it's solved.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 16th, 2009
0

Re: Reading from a .txt file to an ArrayList

My judgement here is that there was no infinite loop at all. When you are running a GUI based program you don't get to see any exceptions thrown by the JVM. Now, when your index counter was reaching 'size' then the JVM would have been throwing an exception which since your program being a GUI program you wouldn't have had an oppurtunity to see. This happens becuase the exception is thrown to the standard output stream which is typically the console. Since the program halted at that instruction the instruction
java Syntax (Toggle Plain Text)
  1. CompSciAPPane.showMessageDialog(All, APLabel, "Computer Science AP", JOptionPane.PLAIN_MESSAGE);
never got executed and hence you never got to see the JOption Pane which led you to believe that since there was no output, there must have been an infinite loop somewhere.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Feb 16th, 2009
0

Re: Reading from a .txt file to an ArrayList

Thanks. I got it all figured out. (at least that one segment!)
Reputation Points: 10
Solved Threads: 1
Light Poster
BrianK123 is offline Offline
25 posts
since Feb 2009
Feb 16th, 2009
0

Re: Reading from a .txt file to an ArrayList

Okay if your query is solved, mark the thread as solved so that others are aware of it.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Java Web Crawler
Next Thread in Java Forum Timeline: number limit





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


Follow us on Twitter


© 2011 DaniWeb® LLC