| | |
Reading from a .txt file to an ArrayList
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
and the troublesome code segment:
Please help me!
The .txt file I am trying to read from:
Java Syntax (Toggle Plain Text)
Mary Anne A Joe Wright B Andrew Heath A
and the troublesome code segment:
Java Syntax (Toggle Plain Text)
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!
I think I solved it. I changed the to
That should be it. I spent hours trying to solve the problem and all it was was a little = sign!
Java Syntax (Toggle Plain Text)
stop=CompSciAP.size(); index=0; while (index<=stop)
Java Syntax (Toggle Plain Text)
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
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.
java Syntax (Toggle Plain Text)
CompSciAPPane.showMessageDialog(All, APLabel, "Computer Science AP", JOptionPane.PLAIN_MESSAGE);
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
![]() |
Similar Threads
- Reading lines from a text file into an array list (C#)
- Writing ArrayList to textFile? (C#)
- Reading from a file into an arraylist (Java)
- reading a part of a txt file (Java)
- no luck with reading integers from file in arraylist (Java)
- WritetoDisk method using ArrayList (Java)
- read info from txt file (Java)
- Need Help >> I Give Up , Cant Do This Program :'((( (Java)
- cpu scheduling I don't know why is not working (Java)
Other Threads in the Java Forum
- Previous Thread: Java Web Crawler
- Next Thread: number limit
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





