1
import java.util.Scanner;

public class SecretWord {
  public static void main( String[] args ) {

    Scanner input = new Scanner(System.in);
    String secret = "Please", guess;

    System.out.print( "Secret word?" );
    guess = input.next();

    for (int i = 0; guess.equals(secret); i++) {
      if( guess.equals(secret) ) {
        System.out.println("enter");
      } else {
        System.out.println( "try again" );
      }
    }
  }
}

How do I make it so that, when a user enters anything other than "Please", it will ask him/her to try again? Then the user will have to enter "Please", end the loop, and print "Enter".

4 Answers 4

1

You have to move the input.next() inside of the loop and I would recommand to use a while instead of a for loop:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String secret = "Please", guess = "";

    System.out.print("Secret word?");

    while (!guess.equals(secret)) {
        guess = input.next();

        if (guess.equals(secret)) {
            System.out.println("enter");
        } else {
            System.out.println("try again");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use a while loop instead,

while (!guess.equals(secret)) {
    if( guess.equals(secret) ) {
        System.out.println( "enter" );
    } else {
        System.out.println( "try again" ); {
        System.out.println("Secret word")
        guess = input.next();
    }
}

Apart from this, the for loop have the following syntax,

for (before, conditionsIsTrue, end)

This means that for you the loop will be like this,

for(int i=0; if(guess.equals(secret)), i++)

Since this condition will never hold for the first loop you will never enter the for loop at all.

You can also use do-while which uses a post test,

do {
    System.out.println("Secret word")
    guess = input.next();
    if( guess.equals(secret) ) {
        System.out.println( "enter" );
    } else {
        System.out.println( "try again" ); {
    }
} while (!guess.equals(secret));

1 Comment

A fun exercise can also be to randomly pick a word in a file and let the user try to guess the word with as few tries as possible. This would likely force the user to write a program to optimize this. From your side, this require that you use "random access from file" in java. To pick a word from some place in the text. Random Access means here that you can access any part of the file without going through the file from top to bottom. It does not mean that a random word is picked. This can be done by generating random numbers instead.
0

Try This:

 import java.util.Scanner;

    public class SecretWord {
      public static void main( String[] args ) {

        Scanner input = new Scanner(System.in);
        String secret = "Please", guess;

        while(true){
            System.out.print( "Secret word?" );
            guess = input.next();
            if(guess.equals(secret)){
                System.out.println("Enter");
                break;
            }else{
                System.out.println("Try Agian");
            }
        }
      }
    }

Comments

0

This is one of the classic examples where the use of 'do-while' construct is encouraged(and there are a very few). But it seems that you explicitly want to use 'for', therefore following is the code using 'for':

import java.util.Scanner;

public class SecretWord {
public static void main( String[] args ) {

  Scanner input = new Scanner(System.in);
  String secret = "Please", guess = null ;

  for ( ; !secret.equals(guess);  ) {
      System.out.print( "Secret word?" );
      guess = input.next();

      if( guess.equals(secret) ) {
          System.out.println( "enter" );
      }
      else {
          System.out.println( "try again" ); {
      }
  }}}}

I hope this works. Now comes the implementation using 'do-while' :

import java.util.Scanner;
public class ModifiedSecretWord {
  public static void main( String[] args ) {

      Scanner input = new Scanner(System.in);
      String secret = "Please", guess;

      do{
          System.out.print( "Secret word?" );
          guess = input.next();  
      }
      while( !guess.equals(secret) ) ;

      System.out.println("Shazam!!!");
}}

1 Comment

Thanks! I didn't know about do_while loops. That's very helpful.

Your Answer

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