Results 1 to 14 of 14
  1. #1
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default Deleting from an array

    Hey again...

    i am trying to delete an index from my array.
    everyting goes well BUT when i look at my index AGAIN i see that index1 is replaced by the index i removed.
    Code looks fine... can't see whats wrong with it.

    Can any1 help me with this?

    Code:
    Java Code:
            Button Code:
            baasjes.get(lstBaasjes.getSelectedIndex()).removeHuisdier(cmbDieren.getSelectedIndex());
            cmbDieren.removeItemAt(cmbDieren.getSelectedIndex());
    
            public void removeHuisdier(int index) {
            while(pets[index+1] != null && index <= 18) {
                pets[index] = pets[index+1];
                index++;
            }
            pets[index] = null;
        }


    More info about the project:

    I add a customer with pets, i select the customer from a JList and the name and regnumber get sent to a combo box(pets).

    when i select a name from the combobox pets and press delete, then it works great.

    BUT when i select the same customer again and look at combobox pets i see that the one i deleted replaces the first index and the first index gets trowen away.
    Last edited by Dieter; 09-22-2009 at 11:40 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Not too clear on what's going on, but:
    Java Code:
            public void removeHuisdier(int index) {
            while(pets[index+1] != null && index <= 18) {
                pets[index] = pets[index+1];
                index++;
            }
            pets[index] = null;
        }
    The while loop here is moving everything to the "left" and overwriting the selected index, ie deleting it. Which is presumably what you want. Finally nullifying the last element. However, what is the size of your array? Hardcoding 18 in there implies it is always 19 (why not pets.length, by the way?). Except you do that check after checking on "index + 1". You'll get an index out of bounds exception if you have a full array and you try to delete the last entry.

    Anyway, barring that little bit of logic this looks OK...so how are you passing this back to whatever holds the list of pets for each customer?

  3. #3
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    if i understand you right, you are asking how i add my pets to the customers?

    if so this is the code:

    Java Code:
    import java.util.ArrayList;
    import javax.swing.DefaultListModel;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Dieter
     */
    
    public class frmMain extends javax.swing.JFrame {
       
        public ArrayList<Baasje> baasjes = new ArrayList<Baasje>();
       
        public DefaultListModel lijstBaasjes = new DefaultListModel();
    
        /** Creates new form frmMain */
    
        public frmMain() {
            initComponents();
        }

    i make a new customer...

    Java Code:
    private void btnAddBaasjeActionPerformed(java.awt.event.ActionEvent evt) {                                             
            boolean exists = false;
    
            if(txtNaamBaasje.getText().equals("") || txtVoornaam.getText().equals("") || txtRijksNrBaasje.getText().equals("") || txtGemeente .getText().equals("") || txtLand.getText().equals("") || txtNr.getText().equals("") || txtPostcode.getText().equals("") || txtProvincie.getText().equals("") || txtStraat.getText().equals("")){
                JOptionPane.showMessageDialog(this, "Gelieve alle verplichte velden in te vullen.", "Foutmelding", JOptionPane.WARNING_MESSAGE);
            }else{
                for(int i=0; i<=lstBaasjes.getMaxSelectionIndex(); i++) {
                    if((txtRijksNrBaasje.getText().equals(baasjes.get(i).getRijksNr()) && (lstBaasjes.getMaxSelectionIndex() >= 0))) {
                        exists = true;
                        Contactgegevens tempGeg = new Contactgegevens(txtStraat.getText(), txtNr.getText(), Integer.parseInt(txtPostcode.getText()), txtGemeente.getText(), txtProvincie.getText(), txtLand.getText(), txtTelefoon.getText(), txtGsm.getText(), txtEmail.getText());
                        baasjes.get(i).setContact(tempGeg);
                        baasjes.get(i).setNaam(txtNaamBaasje.getText());
                        baasjes.get(i).setVoornaam(txtVoornaam.getText());
                        baasjes.get(i).setGeslacht(cmbGeslachtBaasje.getSelectedIndex()==0?true:false);
                        baasjes.get(i).setRijksNr(txtRijksNrBaasje.getText());
                    }
                }
                if(!exists) {
                    Contactgegevens tempGeg = new Contactgegevens(txtStraat.getText(), txtNr.getText(), Integer.parseInt(txtPostcode.getText()), txtGemeente.getText(), txtProvincie.getText(), txtLand.getText(), txtTelefoon.getText(), txtGsm.getText(), txtEmail.getText());
                    Baasje tempBaasje = new Baasje(txtNaamBaasje.getText(), txtVoornaam.getText(), ((cmbGeslachtBaasje.getSelectedIndex()==0)?(true):(false)), txtRijksNrBaasje.getText(), tempGeg);
                    baasjes.add(tempBaasje);
                    lijstBaasjes.addElement(tempBaasje);
                    lstBaasjes.setModel(lijstBaasjes);
                    cmbVoegToeAan.addItem(makeObj(tempBaasje.toString()));
                    lstBaasjes.setSelectedIndex(lstBaasjes.getLastVisibleIndex());
                }
            }
            fillCmbDieren(baasjes.get(lstBaasjes.getSelectedIndex()));
            JOptionPane.showMessageDialog(this, "Baasje " + baasjes.get(lstBaasjes.getSelectedIndex()).getVoornaam() + " " + baasjes.get(lstBaasjes.getSelectedIndex()).getNaam() + "werd succesvol toegevoegd.", "Succesvol toegevoegd", JOptionPane.INFORMATION_MESSAGE);
        }

    after i made that i add the pet...

    Java Code:
      private void btnAddPetActionPerformed(java.awt.event.ActionEvent evt) {                                          
            int geselecteerdBaasje = cmbVoegToeAan.getSelectedIndex();
            boolean exists = false;
            int i = 0;
            if(txtRegNr.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Gelieve alle verplichte velden in te vullen.", "Foutmelding", JOptionPane.WARNING_MESSAGE);
            }else{
                while(baasjes.get(geselecteerdBaasje).getPets(i) != null) {
                    if(baasjes.get(geselecteerdBaasje).getPets(i).getRegNr().equals(txtRegNr.getText())) {
                        JOptionPane.showMessageDialog(this, baasjes.get(geselecteerdBaasje).getVoornaam() + " " + baasjes.get(geselecteerdBaasje).getNaam() + " heeft al een huisdier met als registratienummer " + txtRegNr.getText() + ".", "Foutmelding", JOptionPane.WARNING_MESSAGE);
                        exists = true;
                        break;
                    }
                    i++;
                }
    
                if(!exists) {
                    switch(cmbDiersoort.getSelectedIndex()) {
                        case 0:
                            Hond tempHond = new Hond(txtRegNr.getText(), ((cmbGeslachtHuisdier.getSelectedIndex()==0)?(true):(false)), chkGetraind.isSelected(), txtNaamHuisdier.getText(), chkIngeent.isSelected());
                            baasjes.get(geselecteerdBaasje).addHuisdier(tempHond);
                            break;
                        case 1:
                            Kat tempKat = new Kat(txtNaamHuisdier.getText(), txtRegNr.getText(), ((cmbGeslachtHuisdier.getSelectedIndex()==0)?(true):(false)), chkIngeent.isSelected());
                            baasjes.get(geselecteerdBaasje).addHuisdier(tempKat);
                            break;
                    }
                    fillCmbDieren(baasjes.get(lstBaasjes.getSelectedIndex()));
                }
            }
            JOptionPane.showMessageDialog(this, "Huisdier " + txtRegNr.getText() + " werd succesvol toegevoegd aan baasje " + baasjes.get(lstBaasjes.getSelectedIndex()).getVoornaam() + " " + baasjes.get(lstBaasjes.getSelectedIndex()).getNaam() + ".","Succesvol toegevoegd" , JOptionPane.INFORMATION_MESSAGE);
            wisVelden(false);
        }                                         
    
        public void fillCmbDieren(Baasje baas) {
            int i = 0;
            cmbDieren.removeAllItems();
            while(baas.getPets(i) != null) {
                cmbDieren.addItem(makeObj(baas.getPets(i).getRegNr()+ " " + ((baas.getPets(i).getNaam().equals(""))?(""):("(")) + baas.getPets(i).getNaam() + ((baas.getPets(i).getNaam().equals(""))?(""):(")"))));
                i++;
            }
        }
    if you don't understand everyting i completely understand, i am dutch so i use dutch words to understand it better. say Baasje is customer and Dieren is animals. hope this little info helps.
    Last edited by Dieter; 09-22-2009 at 04:02 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    OK, I was being a bit simple there. The array pets[] is in Baasje, so the remove code above is removing it from the model.

    How do you populate cmbDieren list?
    Or is that also simply referring to the same array?
    I'm just trying to get an idea of where the array is getting messed up.

    To be honest, this is something you might want to debug through. Check the values in the particular Baasje of pets[] after deleting one. Then check after selecting again. That sort of thing.

  5. #5
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    i allready did a system.out.println to see whats he's showing
    but i got it working now.
    i made a new project and copied my .java files in there.

    sorry for wasting your time, should have tried that earlier.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Don't worry.
    At least it works now.
    I'm guessing it was some mix up in the environment that shifting the code helped clear it out?

  7. #7
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    it are allways the logical stuff that make me strugle in java programming :)

    guess thats someting i need to pay attention at now thats its still low level programming :)

    thanks though for your time :) and sorry again for posting it quicker then i think

    all the best,

    Dieter

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Dieter, you should stick with one language. :) Dutch is a great language, obviously (I'm Dutch too ;)), but try not to mix it with other languages; removeHuisdier? xD It would be easier for both you and others if you stick with one language.

    ~Matt
    I die a little on the inside...
    Every time I get shot.

  9. #9
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    hehe, yea i see what you mean :p but i made it so they know what the actionListeners do :p

    was just too lazy to translate it all to english:)

    thx tho for the tip ;)
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Put it through babel fish...:)

  11. #11
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Oh yea, that will give you great translations.., not. :)
    I die a little on the inside...
    Every time I get shot.

  12. #12
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    haha:p ahwell ppl wer able to help me, so big deal :p
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    And I got to learn a couple of new words...except I'll never actually remember them.

  14. #14
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    hehe, i can learn u a few words..
    piemel means hello in slang :p
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

Similar Threads

  1. Help deleting a file
    By 3speed in forum New To Java
    Replies: 4
    Last Post: 11-01-2008, 05:27 AM
  2. Deleting from an object
    By vitaminz in forum New To Java
    Replies: 7
    Last Post: 08-10-2008, 03:56 AM
  3. Deleting Contractions
    By theonly in forum New To Java
    Replies: 3
    Last Post: 04-27-2008, 07:44 PM
  4. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:42 AM

Posting Permissions

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