Reading from a .txt file to an ArrayList

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

Join Date: Feb 2009
Posts: 25
Reputation: BrianK123 is an unknown quantity at this point 
Solved Threads: 1
BrianK123's Avatar
BrianK123 BrianK123 is offline Offline
Light Poster

Reading from a .txt file to an ArrayList

 
0
  #1
Feb 15th, 2009
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:
  1. Mary Anne
  2. A
  3. Joe Wright
  4. B
  5. Andrew Heath
  6. A

and the troublesome code segment:
  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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 25
Reputation: BrianK123 is an unknown quantity at this point 
Solved Threads: 1
BrianK123's Avatar
BrianK123 BrianK123 is offline Offline
Light Poster

Re: Reading from a .txt file to an ArrayList

 
0
  #2
Feb 15th, 2009
I think I solved it. I changed the
  1. stop=CompSciAP.size();
  2. index=0;
  3. while (index<=stop)
to
  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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Reading from a .txt file to an ArrayList

 
0
  #3
Feb 15th, 2009
nice work, althought I wouldn't think that would cause a infinite, more likely an ArrayIndexOutOfBounds... anyway, it's solved.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Reading from a .txt file to an ArrayList

 
0
  #4
Feb 16th, 2009
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
  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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 25
Reputation: BrianK123 is an unknown quantity at this point 
Solved Threads: 1
BrianK123's Avatar
BrianK123 BrianK123 is offline Offline
Light Poster

Re: Reading from a .txt file to an ArrayList

 
0
  #5
Feb 16th, 2009
Thanks. I got it all figured out. (at least that one segment!)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Reading from a .txt file to an ArrayList

 
0
  #6
Feb 16th, 2009
Okay if your query is solved, mark the thread as solved so that others are aware of it.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC