Gaetano
Gaetano

Reputation: 29

Don't duplicate character - Java

I'm trying to have the program print out an error if the character inputted has already been used. If you check the else If statement, it technically runs but I don't want it to print each time. Only when the character has been chosen in another round.

while (!userCorrect) {
    Scanner input = new Scanner(System.in);
    System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
    String letters = input.nextLine();
    createString(letters.length());
    if (letters.length () > 1) { //code will print an error if the user selects more than 1 character
        System.out.println("You should not enter more than 1 character");
    }
    else if (letters.length()==letters.length()) { //print error if the character has been chosen already
        System.out.println(letters + " has been chosen before, try again");
    }
    else {
        System.out.println("end (round " + roundNumber + ")");
        roundNumber++; 
    }
}

The closest I got was letters.length() == letters.length(); but I know that doing this will always print the error. Do you think there's a parameter I can put in there that'll make it skip the first round and only start looking when the second round starts?

Upvotes: 0

Views: 181

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51892

I know you already have an answer but I wanted to point out a few things

  • Don't create a new Scanner object each time in the loop
  • Use continue to avoid nested if-else
  • There are a number of ways to store the characters, like using a List or a Set collection or a StringBuffer or a simply a String.
  • name your boolean variables is...
  • I ignored the createString method and skipped the userCorrect since they are unclear how to use

(I also ended my own exit condition to the loop since just to have one)

Scanner input = new Scanner(System.in);
int roundNumber = 1;
boolean isUserCorrect = false;

String selected = "";

while (true) {
    System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
    String letters = input.nextLine();

    if (letters.length () > 1) { //code will print an error if the user selects more than 1 character
        System.out.println("You should not enter more than 1 character");
        continue;
    }

    if (selected.contains(letters)) {
        System.out.println(letters + " has been chosen before, try again");
        continue;
    }

    selected = selected.concat(letters);
    System.out.println("end (round " + roundNumber + ")");
    roundNumber++;
    if (roundNumber > 5) {
        System.out.println("Selected characters: " + selected);
        break;
    }
}
input.close();

Upvotes: 1

Belham
Belham

Reputation: 165

You should use a list where you store the chars, then look if you used the current one before or not.

I think this will help you :

List<Character> choosedCharacters = new ArrayList<>();
while (!userCorrect) {
    Scanner input = new Scanner(System.in);
    System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
    String letters = input.nextLine();
    createString(letters.length());
    if (letters.length () > 1) { //code will print an error if the user selects more than 1 character
        System.out.println("You should not enter more than 1 character");
    }
    else if (choosedCharacters.contains(letters.charAt(0))) { //print error if the character has been chosen already
        System.out.println(letters + " has been chosen before, try again");
        choosedCharacters.add(letters.charAt(0));
    }
    else {
        System.out.println("end (round " + roundNumber + ")");
        choosedCharacters.add(letters.charAt(0));
        roundNumber++;
    }
}

Upvotes: 1

Related Questions