-1

I'm fairly new at programming. Currently im working on a uni project to create a basic text game using java. The problem im having is trying to figure out how to implement a business rule that does not allow a user to enter the same name. I have it set up so the scanner reads into the array. I'm using Java and this is my first time using the forum so i greatly appreciate any help given and thank all of you in advance!:)

And apologies for the poor formatting i dont know how to post properly yet.

        try {
            
            // Takes in the number of players 
            int noOfPlayers;
            Scanner s = new Scanner(System.in);
            System.out.println("Enter number of players between 2 and 4");
            noOfPlayers = s.nextInt();

            s.nextLine();
            if (noOfPlayers < 2 || noOfPlayers >= 5) { // limits number of players enter no less than 2 and no greater than 4
                System.out.println("Nope wrong number");
                enterInfo();
                s.close();
                return;
            } else {
                
                // array for storing player names

                String[] names = new String[noOfPlayers];
                
                // iterates through the array depending how many players are selected
                // and takes in String input

                for (int counter = 0; counter < noOfPlayers; counter++) {
                    System.out.println("Enter name of player : " + (counter + 1));
                    names[counter] = s.nextLine();
                    
                    
                    // fix this to stop same name sbeing entered 
                    //if(names.equals(names)) {
                    //  System.out.println("Enter a different name");
                    //  counter--;
                //  }
                    
                    

                }

            }
            
            s.close();
        } catch (Exception e) {
            System.out.println("Problem");

        }

    }
Pearse
  • 1
  • 1
  • You seem to know how to use loops, so you need to check if the newly entered name is available in `names` array just using another loop `for int j = 0; j < counter; j++)` and `if` statement to compare the names. If a name is not found, then add it to `name` and increase `counter`. – Nowhere Man Feb 21 '22 at 15:26
  • Hi Alex, i wasn't expecting a response so soon, I appreciate the help thank you! Can i ask how do i check if the name is available? What would the code look like to do something like that? – Pearse Feb 21 '22 at 15:31

1 Answers1

0

How about use Set? Set has to have all different things.

Like in Array [kim,lee,kim,park] but in Set [kim,lee,park]

input kim => Array [kim] set[kim] lenth 1 == size 1

input lee => Array [kim,lee] set[kim,lee] lenth 2 == size 2

input kim => Array [kim,lee,kim] set[kim,lee] lenth 3 != size 2

then all you have to do is tell them "make another name plz"

String name = s.nextLine();
names[counter] = name;

Set<String> set = new HashSet<String>(); 
set.add(name)

if(names.length != set.size())
{
 System.out.println("Enter a different name");
 counter--;
}

I hope you understand my poor explanation.

z961223
  • 1
  • 2
  • Thanks Z, I will give it a shot, only last week i learned the topic of hashsets, so im still coming to terms with it but ill give it a go! Explanation is perfect all the code makes sense! – Pearse Feb 21 '22 at 15:38