1

I've a list of words (Object) in an array. Each word has a number, word, and hint. (Please note, the number is 1 more than the index in the array.) I want the user to be able to delete an item in the array. I have written a method which reads the users input (int) the word, and hint at the index of the array, takes the value of the hint and word into the next array, and then that one will take the hint and word of the one after that etc. I have written the method, but every time I delete a word, any words after that take the word and hint of the last object in the array.

eg: first it's like

1 dog bark

2 cat meow

3 cow moo

4 chicken cluck

5 pig oink

after the user deletes the word at 3

1 dog  bark

2 cat meow

3 pig oink

4 pig oink

Can anyone tell me what is the problem, please?

Code:

public void deleteWord() throws IOException {

    if (wCount > 0) {
        int again = JOptionPane.YES_OPTION;
        while (again == JOptionPane.YES_OPTION) {
            int num = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of the word you wish to delete", "Enter word number", JOptionPane.PLAIN_MESSAGE))-1;
            int cnfrm = JOptionPane.showConfirmDialog(null, "Are you sure you wish to delete the word:" + "\n" + "\t" + wArr[num].getWrd(), "Are you sure?", JOptionPane.YES_NO_OPTION);
            if (cnfrm == JOptionPane.YES_OPTION) {
                for (int i = num; i < (wCount - 1); i++) {
                    for (int j = (i + 1); j < wCount; j++) {
                        wArr[i].setWrd(wArr[j].getWrd());
                        wArr[i].setHnt(wArr[j].getHnt());
                    }
                }
                wCount--;
                wArr[wCount] = null;
            }
            PrintWriter pw = new PrintWriter(new FileWriter("words.txt", false));
            for (int x = 0; x < wCount; x++) {
                pw.println(wArr[x].toString(1));
            }
            pw.close();
            displayWords();
            again = JOptionPane.showConfirmDialog(null, "Do you wish to delete another word?", "Delete another wod?", JOptionPane.YES_NO_OPTION);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Thre are no words to delete", "ERROR", JOptionPane.ERROR_MESSAGE);
    }

}

EDIT:

This is a homework assignment, which obviously means I still don't know a lot about programming, incl. ArrayLists. I will find out about them, but unfortunately this project (Hangman in case you were wondering) is due on Monday, so I will not be implementing it in this program.

1

3 Answers 3

4

Your problem is this part:

for (int i = num; i < (wCount - 1); i++) {
    for (int j = (i + 1); j < wCount; j++) {
        wArr[i].setWrd(wArr[j].getWrd());
        wArr[i].setHnt(wArr[j].getHnt());
    }
}

In the end, it will always replace the content at position i with the one at position wCount - 1. Use the following instead:

for (int i = num; i < (wCount - 1); i++) {
    int j = i + 1;
    wArr[i].setWrd(wArr[j].getWrd());
    wArr[i].setHnt(wArr[j].getHnt());
}

But as suggested in the comments to your question: Why not use a List (like ArrayList) instead?

Sign up to request clarification or add additional context in comments.

Comments

3

You have two loops when you should have one. This sort of bug is easily found using a debugger.

for (int i = num; i < (wCount - 1); i++) {
    for (int j = (i + 1); j < wCount; j++) {
        wArr[i].setWrd(wArr[j].getWrd());
        wArr[i].setHnt(wArr[j].getHnt());
    }
}

For every word from the number you want to delete, you are copying every word up to the end, the last one being the end.

for (int i = num; i < (wCount - 1); i++) {
        wArr[i].setWrd(wArr[i+1].getWrd());
        wArr[i].setHnt(wArr[i+1].getHnt());
}

This copies each value down one, once.

Comments

2

With this algorithm it will get all combinations of (i, j) in the body of the inner loop, surprisingly assigning to element (i) each (j) and finally last of (j).

But you don't need such algorithm to remove an element in the array. You'd better copy remaining part of the array.

Word[] newWArr = new Word[wArr.length - 1];
System.arraycopy (wArr, 0, newWArr, 0, num);
System.arraycopy (wArr, num + 1, newWArr, num, wArr.length - num - 1);
wArr = newWArr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.