954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading from a .txt file to an ArrayList

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:

Mary Anne
A
Joe Wright
B
Andrew Heath
A


and the troublesome code segment:

public class ViewClassesListener implements ActionListener
    {           
        public void actionPerformed (ActionEvent event)
        {
            //entering the class and grade information into arrays
            try
            {
                FileInputStream APStream=new FileInputStream("CompSciAP.txt");
                DataInputStream APDIS = new DataInputStream(APStream);
                BufferedReader APBR=new BufferedReader(new InputStreamReader(APDIS));
                
                String temp="";
                
                while ((temp=APBR.readLine()) != null)
                {
                    //infinite loop?
                    CompSciAP.add(temp);
                }
                APStream.close();
            }
            catch (Exception e)
            {
                System.err.println("Error: " + e.getMessage());
            }
            
            //displaying the information
            stop=CompSciAP.size();
            index=0;
            while (index<=stop)
            {
                //or is this the infinite loop?
                APLabel.setText(APLabel.getText()+CompSciAP.get(index)+"\n");
                index++;
            }
            CompSciAPPane.showMessageDialog(All, APLabel, "Computer Science AP", JOptionPane.PLAIN_MESSAGE);
        }
    }


Please help me!

BrianK123
Light Poster
25 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

I think I solved it. I changed the

stop=CompSciAP.size();
            index=0;
            while (index<=stop)

to

stop=CompSciAP.size();
            index=0;
            while (index<stop)


That should be it. I spent hours trying to solve the problem and all it was was a little = sign!

BrianK123
Light Poster
25 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

nice work, althought I wouldn't think that would cause a infinite, more likely an ArrayIndexOutOfBounds... anyway, it's solved.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

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

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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Thanks. I got it all figured out. (at least that one segment!)

BrianK123
Light Poster
25 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Okay if your query is solved, mark the thread as solved so that others are aware of it.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You