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!

Recommended Answers

All 5 Replies

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!

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.