-2

I need to get inputs to fill an array. My problem is I also need to check if the value I input does not exist already in the array. If exists I need to show a message that says bad grade. I believe I get stuck on the search loop I and Im not able no assign the value to the array If is not already there.

String[] course = new String[9];
int index = 0;

if (menu == 1) {
    boolean found = true;
    do {
        value = (JOptionPane.showInputDialog("Enter course " + (index + 1)));
        int pos = 0;
        while (pos< course.length&& !found) {
            if (value == course[index]) {
                found = true;  
            } else {
                pos++;
            }      
        } // while

        if(found == true) {
           course[index] = value;
        } else {
           course[index]="";
        }

        if (course[index].equals("")) {
            JOptionPane.showMessageDialog(null, "Bad Course Name");
        } else{
            course[index] = (JOptionPane.showInputDialog("Enter course " + (index + 1)));
        }
    } while(course[index].equals("")); //last
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Joe Segway
  • 3
  • 1
  • 4

1 Answers1

1

The problem with your implementation is that once found is set to true, you never reset it back to false: it's a one-way street. That is why entering the first duplicate value prevents other non-duplicated values from being entered.

You can fix this by moving the declaration/initialization of found inside your do/while loop. However, a better approach would be defining a helper method that searches the array for you up to the specific position, and returns true if a duplicate is found:

private static boolean isDuplicate(String[] course, int maxIndex, String entry) {
    ...
}

Now the loop searching for duplicates would be hidden, along with the variable indicating the result. The code becomes more readable, too, because the name of the method tells the reader what happens inside.

Of course, you need to fix your string comparison: your code uses ==, which is not the way it is done in Java.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you. I moved the found inside the do/while loop but still get the same result. The method approach sounds better but I can not solve this by using methods. Thanks for the help. – Joe Segway Mar 25 '16 at 18:00
  • @JoeSegway What do you mean "may not solve this by using methods"? I can understand restricting students to arrays, but not allowing them to use helper methods makes no sense to me: in fact, this is something to encourage! – Sergey Kalinichenko Mar 25 '16 at 18:02
  • I know. I'm just following the instructions – Joe Segway Mar 25 '16 at 18:21
  • @JoeSegway OK then. Did you see the edit to the answer? You know what to fix, right? – Sergey Kalinichenko Mar 25 '16 at 18:22
  • I did but still can't figure it out. I keep getting the same the bad course name regardless what I input . I'm very lost. – Joe Segway Mar 25 '16 at 18:38
  • @JoeSegway Did you fix the declaration to set `found` to `false`? Your current code sets it to `true` initially, which is wrong. – Sergey Kalinichenko Mar 25 '16 at 18:52