0

My task is to enter names into an array. If the name has already been entered, the program must alert about that and offer to reenter the player under the same number.

This is my code:

public void enterNames()    {

     for (int i=0; i<nameOfPlayers.length; i++) 
     {
           do 
           {    
               // isDuplicate is a Boolean initialized to false     
               System.out.println("CHECK": + isDuplicate);

               System.out.println("Enter player " + (i+1) + ":");

               nameOfPlayers[i] = in.next();

               for (int k=0; k<nameOfPlayers.length; k++)   
               {
                   if (k!=i && nameOfPlayers[i].equals(nameOfPlayers[k]))   
                   {
                       isDuplicate = true;                      
                       break;
                   }            
               }
           } while (isDuplicate = false);
      } 
 }

Interesting, even when I enter a duplicate value, it is caught and assigns true to isDuplicate, but when it returns to the beginning of the while loop, the value is false again ("CHECK: false").

Looks like an easy task, but I am caught...

Also, I did not want to use HashSet and wanted to use only Array.

Thanks a lot!

EDIT:

Thanks to others, I rewrote the code to the following:

public void enterNames()    {

    List<String> nameOfPlayersList = new ArrayList<String>();
    int i = 0;

    for (i=0; i<numberOfPlayers;)   
    {
        while(true) 
        {
            System.out.println("Enter player " + (i+1) + ":");
            String input = in.next();

            if(!nameOfPlayersList.contains(input))  
            {
                nameOfPlayersList.add(input);
                i++;
                break;
            }
            System.out.println("Player " + input + " already exists, please retry");
        }
    }
}

3 Answers 3

1

Answer reformed, used List to add more and more elements without pre defined size.

changed while (isDuplicate == false); to while (!isDuplicate);

 public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        List<String> nameOfPlayers = new ArrayList<String>();
        boolean isDuplicate = false;
        int i = 0;

        do {
            System.out.println("Enter player " + (i + 1) + ": or Q for Quit");
            String input = scanner.next();

            if (!input.equalsIgnoreCase("Q")) {
                if (nameOfPlayers.contains(input)) {
                    isDuplicate = true;
                } else {
                    nameOfPlayers.add(input);
                    isDuplicate = false;
                }
                System.out.println("CHECK : " + isDuplicate);
            } else {
                break;
            }

            i++;
        } while (!isDuplicate);
    }


Enter player 1: or Q for Quit
ankur
CHECK : false
Enter player 2: or Q for Quit
singhal
CHECK : false
Enter player 3: or Q for Quit
ankur
CHECK : true
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!. I redid my code in a bit different way, but your solution prompted how to do it.
1

The problem you are having is because of the

} while (isDuplicate = false);

it should be (mind the double ==)

} while (isDuplicate == false);

Apart from that your code is quite inefficient. You would probably do much better with two Arrays if that is really what you want, otherwise a linked list would be best.

1 Comment

Thanks. I completely forgot that while requires == as well. :-)
1

Your while is incorrect, this

while (isDuplicate = false);

assigns false to isDuplicate which has a side-effect of also evaluating to false. You watned something like

while (isDuplicate == false);

or the shorter

while (!isDuplicate);

1 Comment

Thanks. I completely forgot that while requires == as well. :-)

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.