Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By allaudin

Thread: Linked Lists

  1. #1
    bobbythecount is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Linked Lists

    Hello a mate of mine an me are Programming a decoy-program for our it-classes in university.
    We integrated nearly all of the functions that are neccesary, but one is only functioning if i search for entrys, which i entered through code.
    if i read them into the list (creating the same type of object for my understanding), it produces an IndexOutOfBoundsException.

    our code is not near to being perfect, but it mostly does what it shall do.



    Java Code:
    import java.awt.FileDialog;
    import java.awt.Frame;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.text.DateFormat;
    import java.util.Locale;
    import java.util.ArrayList;
    /**
     *
     * @author Phillip Niemeyer & Jasmin Erfurt
     */
    public class Warteschlange extends javax.swing.JFrame {
        private File datei;
    
         private LinkedList fifoListe;
    
        public Warteschlange() {
            initComponents();
            fifoListe = new LinkedList();
    
              
           
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            einfuegenNach = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            ausgeben = new javax.swing.JButton();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
            jLabel1.setText("Warteschlangen Belegaufgabe");
    
            jLabel2.setText("Name, Geburstdatum:");
    
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
    
            einfuegenNach.setText("In Warteschlange einreihen");
            einfuegenNach.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    einfuegenNachActionPerformed(evt);
                }
            });
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            ausgeben.setText("Alles Anzeigen");
            ausgeben.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ausgebenActionPerformed(evt);
                }
            });
    
            jButton1.setText("Mittlere Wartezeit");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Bedienungsbeginn");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("Kürzeste Wartezeit");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            jButton4.setText("Längste Wartezeit");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            jButton5.setText("Leute in der Schlange");
            jButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton5ActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(71, 71, 71)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(jLabel1)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 211, Short.MAX_VALUE))
                                .add(layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, einfuegenNach, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                            .add(jLabel2)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 136, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                    .add(0, 0, Short.MAX_VALUE))))
                        .add(layout.createSequentialGroup()
                            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(ausgeben, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(29, 29, 29)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)))
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(64, 64, 64))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap(13, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(38, 38, 38)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel2)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(einfuegenNach)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(layout.createSequentialGroup()
                            .add(jButton2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jButton3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jButton4)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jButton1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jButton5))
                        .add(jScrollPane1))
                    .add(18, 18, 18)
                    .add(ausgeben)
                    .add(113, 113, 113))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void einfuegenNachActionPerformed(java.awt.event.ActionEvent evt) {                                              
    
    
    String time;
    Date     date = new Date();
    DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    time = formatter.format(date);
    
    
    
         
            Element neu;            //Variable für neues Element
            neu = new Element(jTextField1.getText()+" "+ time);  //neues Element erzeugen
            fifoListe.addLast(neu); 
            
    // Vielleicht noch Abfrage ob Tatsächlich etwas, bzw. Name, Geb. eingetrage wurde
            
            
            jTextField1.setText(""); // Leeren des Eingabefeldes
        }                                             
    
        private void ausgebenActionPerformed(java.awt.event.ActionEvent evt) {                                         
            
            Element temp;     
            jTextArea1.setText("");  //Textfenster leeren
            //Liste durchlaufen
    
            for (ListIterator meinIt = fifoListe.listIterator(); meinIt.hasNext();) {
                temp = (Element) meinIt.next();
                jTextArea1.append(temp.getName() + "\n");
            }
    
        }                                        
    
       private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          
    
            
            int zahlPosition = fifoListe.lastIndexOf(jTextField1.getText());
            int wZeit;
     Element temp;
    ListIterator meinIt = fifoListe.listIterator(zahlPosition);
     temp = (Element) fifoListe.listIterator(zahlPosition);
     wZeit = (zahlPosition*15);
            if (zahlPosition != -1){
    jTextArea1.setText("");
    
     jTextArea1.append(temp.getName()+ "sie sind an " + zahlPosition +"ter Stelle"
             + "sie sind in " + wZeit + "Minuten dran");
    
    
    }
            else 
                jTextArea1.append("bla erstmal");
            
            // Aktuelle Uhrzeit + Dauer der Warteschlange dann ausgabe der Uhrzeit + vorraussichtliche wartezeit
        }                                    
    
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     int lSchlange = fifoListe.size();
                
            jTextArea1.setText(""); 
            
                jTextArea1.append( lSchlange + "\n");
            
        }                                        
    
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int lSchlange = fifoListe.size();
    int lSchlangeMax = lSchlange * 15;
               
            jTextArea1.setText(""); 
            
                jTextArea1.append( lSchlangeMax + "\n");        
        }                                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int lSchlange = fifoListe.size();
    double lSchlangeMitte = ((lSchlange*15D)/2);
    jTextArea1.setText(""); 
    if(lSchlange == 1){
                jTextArea1.append( 15 + "\n");    
    }
    else
        jTextArea1.append( lSchlangeMitte + "\n");  
        }                                        
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
    Element kZeit;
    
    
    kZeit = (Element) fifoListe.getFirst();
    
    
    
    boolean result = fifoListe.isEmpty(); //isEmpty() will return true if List is empty
    
    if(fifoListe.size() == 0){
        jTextArea1.setText("");
       jTextArea1.append("Sie können jederzeit reingehen!");
    }
    else
       
        jTextArea1.setText("");
    jTextArea1.append(kZeit.getName());
    
    
        }                                        
        
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Warteschlange().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton ausgeben;
        private javax.swing.JButton einfuegenNach;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    
    
    
    }
    looking forward to your help, thx in advance!


    ps: the method itself that is the prob:

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {



    int zahlPosition = fifoListe.lastIndexOf(jTextField1.getText());
    int wZeit;
    Element temp;
    ListIterator meinIt = fifoListe.listIterator(zahlPosition);
    temp = (Element) fifoListe.listIterator(zahlPosition);
    wZeit = (zahlPosition*15);
    if (zahlPosition != -1){
    jTextArea1.setText("");

    jTextArea1.append(temp.getName()+ "sie sind an " + zahlPosition +"ter Stelle"
    + "sie sind in " + wZeit + "Minuten dran");


    }
    else
    jTextArea1.append("bla erstmal");

    // Aktuelle Uhrzeit + Dauer der Warteschlange dann ausgabe der Uhrzeit + vorraussichtliche wartezeit
    }

    ps2: the error


    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: -1, Size: 3
    at java.util.LinkedList.checkPositionIndex(LinkedList .java:558)
    at java.util.LinkedList.listIterator(LinkedList.java: 865)
    at Warteschlange.jButton2ActionPerformed(Warteschlang e.java:227)
    at Warteschlange.access$400(Warteschlange.java:24)
    at Warteschlange$5.actionPerformed(Warteschlange.java :94)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
    Last edited by bobbythecount; 08-15-2013 at 07:53 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Linked Lists

    You researched what an IndexOutOfBoundsException is right? You get that when you try to read from a position in the list that does not even exist. So say your list is 5 elements in size, trying to get something from position 5 is already going to cause the exception. Reading from a negative position is also going to cause it.

    Now follow the code you quoted. You have this piece of code in there that checks if the zahlPosition is not -1, so you already knew that it can happen that this value is returned.

    Java Code:
    if (zahlPosition != -1){
      jTextArea1.setText("");
      ...
    Now before you perform this check, you are already using the zahlPosition... that might be -1 which is not a valid index into the list...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    bobbythecount is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Linked Lists

    thx man, that does not solve my problem at all, but i see the point and at least i could work around the exception now.
    thx a lot!

    sincerly

    bobby

  4. #4
    bobbythecount is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Linked Lists

    hi again, the problem evolved :)
    now from the lovely IndexoutofBoundsException we moved to...

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.LinkedList$ListItr cannot be cast to Element
    what i don't get is the following: in the method:

    Java Code:
    private void ausgebenActionPerformed(java.awt.event.ActionEvent evt) {                                         
            
            Element temp;     
            jTextArea1.setText("");  //Textfenster leeren
            //Liste durchlaufen
    
            for (ListIterator meinIt = fifoListe.listIterator(); meinIt.hasNext();) {
                temp = (Element) meinIt.next();
                jTextArea1.append(temp.getName() + "\n");
            }
    
        }
    i cast the listiterator to an Element

    and now in my problem-method:


    Java Code:
     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                  
            int zahlPosition = fifoListe.indexOf(jTextField1.getText());
               if (zahlPosition != -1){
                     int wZeit;
                 Element temp;
                ListIterator meinIt = fifoListe.listIterator(zahlPosition);
     temp = (Element) meinIt;
     wZeit = (zahlPosition*15);
                
                
    jTextArea1.setText("");
    
     jTextArea1.append(temp.getName()+ "sie sind an " + zahlPosition +"ter Stelle"
             + "sie sind in " + wZeit + "Minuten dran");
    
    
    }
            else 
                
                jTextArea1.setText("");
                jTextArea1.append("Sie sind leider nicht eingetragen");
            
            // Aktuelle Uhrzeit + Dauer der Warteschlange dann ausgabe der Uhrzeit + vorraussichtliche wartezeit
        }
    i try the same ( i thought) but it does not work.
    i am pretty clueless (again).

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Linked Lists

    At line 8 you are casting listIterator to Element .. try to replace this line by meinIt.next() ,
    gimbal2 likes this.

Similar Threads

  1. Linked lists
    By Malv in forum New To Java
    Replies: 12
    Last Post: 08-15-2013, 03:36 PM
  2. help with linked lists
    By akusa in forum New To Java
    Replies: 1
    Last Post: 04-15-2013, 04:34 AM
  3. Linked Lists
    By ashplusham in forum New To Java
    Replies: 3
    Last Post: 11-20-2012, 08:05 PM
  4. Linked Lists
    By Dee in forum New To Java
    Replies: 18
    Last Post: 02-02-2011, 03:14 AM
  5. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 08:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •