Question

1) The first loop I need to use is a do/while. In this loop the user is prompted to type in a word. If the user types in the wrong word they get an error message:

Invalid! Try Again! 2 attempt(s) left!

Invalid! Try Again! 1 attempt(s) left!

Sorry! You have no more attempts left!

This output works as long as the wrong word is entered each time, but if the correct word is entered after 1 failed attempt it still applies the error message

2) Within my second loop (for loop) the user is asked to enter "3 * 8 = " This portion of the loop works fine if a wrong number is entered all 3 times or if 24 is entered on any attempt.

The problem lies in the loop after 24 is entered. The output is as follows:

Thank you blah blah. We'll call you at 5555555555 if you're a winner. 3 * 8 = Where the 3 * 8 should not be showing. I realize I could enter a break; after this statement, but the instructions specifically say that I cannot use the break command.

The correct output should read: Thank you blah blah. We'll call you at 5555555555 if you're a winner.

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);

    int attempt = 2;
    int answer = 24;
    long phoneNumber = 0;
    String firstName = "";
    String lastName = "";
    String wordOfTheDay = "";

    System.out.printf("Enter the word of the day:  ");
    wordOfTheDay = input.nextLine();

    if(wordOfTheDay.equals("tired"))
    {
        for( attempt = 2; attempt >= 0; --attempt)
        {
            System.out.print(" 3 * 8 = ");
            answer = input.nextInt();
            input.nextLine();

            if( answer == 24)
            {
                System.out.printf( "Please enter your first name, last name, and phone number (no dashes or spaces)\n"                              +"in a drawing for an all-expenses-paid vacation in the Bahamas: " );

                firstName = input.next();
                lastName = input.next();
                phoneNumber = input.nextLong();

                System.out.printf(
                    "Thank you %s %s. We'll call you at %d if you're a winner.",
                    firstName,
                    lastName,
                    + phoneNumber);
            }

            else if( answer != 24)
            {
                if(attempt!=0)
                {
                    System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
                    continue;
                }
                else
                {
                    System.out.print( "Sorry!  You have no more attempts left!" );
                }
            }
        }
    }
    else
    {
        do
        {
            System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
            --attempt;
            System.out.printf("Enter the word of the day:  ");
            wordOfTheDay = input.nextLine();
        } while (attempt >= 1);

        if( attempt == 0)
        {
            System.out.print( "Sorry!  You have no more attempts left!" );
        }
    }
}

I hope I made this clear enough.

To recap, I need to fix the problem with my do/while not letting me enter the correct word after a failed attempt.

Also, I need to get rid of the 3 * 8 = showing up after the users enters the correct input.

Was it helpful?

Solution

Normally, you would use the break statement, but as you are not allowed setting attempt = -1 will have the same effect:

if( answer == 24)
{
    ...
    attempt = -1; // An ugly 'break'
}

EDIT:

Move the do { } while(); to before if check:

    // These two lines of code are no longer required.
    //System.out.printf("Enter the word of the day:  ");
    //wordOfTheDay = input.nextLine();

    do
    {
        System.out.printf("Enter the word of the day:  ");
        wordOfTheDay = input.nextLine();

        if(!wordOfTheDay.equals("tired"))
        {
            System.out.printf(
                "Invalid! Try Again! %d attempt(s) left!\n ", --attempt);
        }
        else
        {
            attempt = 0; // Another ugly 'break'
        }
    } while (attempt >= 1);


    if(wordOfTheDay.equals("tired"))
    {
    }
    // Remove else branch as not required.

OTHER TIPS

I think you're trying to do this.!

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

        int answer = 0;
        long phoneNumber = 0;
        String firstName = "";
        String lastName = "";
        String wordOfTheDay = "";

        int mainMenuAttempt = 3;
        do {
            System.out.printf("Enter the word of the day:  ");
            wordOfTheDay = input.nextLine();

            if (wordOfTheDay.equals("tired")) {

                int answerAttempt = 3;
                do {
                    System.out.print(" 3 * 8 = ");
                    answer = input.nextInt();
                    input.nextLine();
                    answerAttempt--;

                    if (answer != 24 && answerAttempt >0)
                        System.out.printf(
                                "Invalid! Try Again! %d attempt(s) left!\n ",
                                answerAttempt);

                } while (answerAttempt >0 && answerAttempt < 3 && answer != 24);

                if (answer == 24) {
                    System.out
                            .printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n"
                                    + "in a drawing for an all-expenses-paid vacation in the Bahamas: ");

                    firstName = input.next();
                    lastName = input.next();
                    phoneNumber = input.nextLong();

                    System.out
                            .printf("Thank you %s %s. We'll call you at %d if you're a winner.",
                                    firstName, lastName, +phoneNumber);
                }

            }

            mainMenuAttempt--;

        } while (mainMenuAttempt >0 && mainMenuAttempt < 3 && !wordOfTheDay.equals("tired") && answer!=24);
        System.exit(0);
    }
}

Try breaking the whole thing down into very small functions that do one specific thing each. For example, you might have a function that just checks if a word is the correct one, returning 0 if it is, or the number of attempts made if it isn't.

private int checkWord(String correctWord, String wordToCheck, int attempts)
{
    if(wordToCheck.equals(correctWord))
    {
        return 0;
    }
    return attempts;
}

You could then make a function which takes user input, and calls this function to check said input. It could then output an error message dependant on the return code of the previous function. The function would then return a code to tell the function above it the situation.

public int checkWordVerbose(String question, String correctWord, int attempts)
{
    if(attempts <= 3)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print(question);
        String input = scan.nextLine();
        int failureCode = checkWord(correctWord, input, attempts);

        if(failureCode == 0)
        {
            return 0;
        } 
        else
        {
            System.out.println("Invalid! Attempts left: " + (3 - failureCode));
            return 1;
        }
    }
    else
    {
        System.out.println("Sorry! You have no more attempts left!\n");
        return 2;
    }
}

Lastly, you could make a function which simply contains the loop, and holds the logic for maintaining that loop:

public int checkWord(String correctWord)
{
    int failureCode = 1;
    int attempts = 1;
    do
    {
        failureCode = checkWordVerbose("Enter the word of the day: ", correctWord, attempts);
        attempts++;
    } while(failureCode == 1);
    return failureCode;
}

Then in main, all you need to do is check if checkWord(String correctWord) returned 0. Repeat the same thing for your other check (Or you might even be able to use some of the same functions), performing another if inside the first, eg:

if(checkWord("tired") == 0)
{
    if(checkMath("24") == 0)
    {
        // Success!
    }
}

Unless I'm missing something, you are waiting for the first input and if it's wrong, you are going in a do-while loop and then never checking again if the input is correct.

You have to move the if inside the do-while loop:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int attempt = 2;
    int answer = 24;
    long phoneNumber = 0;
    String firstName = "";
    String lastName = "";
    String wordOfTheDay = "";

    System.out.printf("Enter the word of the day:  ");
    wordOfTheDay = input.nextLine();
    do {
        if (wordOfTheDay.equals("tired")) {

            for (attempt = 2; attempt >= 0; --attempt) {
                System.out.print(" 3 * 8 = ");
                answer = input.nextInt();
                input.nextLine();

                if (answer == 24) {
                    System.out
                            .printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n"
                                    + "in a drawing for an all-expenses-paid vacation in the Bahamas: ");

                    firstName = input.next();
                    lastName = input.next();
                    phoneNumber = input.nextLong();

                    System.out
                            .printf("Thank you %s %s. We'll call you at %d if you're a winner.",
                                    firstName, lastName, +phoneNumber);
                }

                else if (answer != 24) {

                    if (attempt != 0) {

                        System.out
                                .printf("Invalid! Try Again! %d attempt(s) left!\n ",
                                        attempt);
                        continue;
                    } else {

                        System.out
                                .print("Sorry!  You have no more attempts left!");
                    }
                }

            }

        } else {

            System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ",
                    attempt);
            System.out.printf("Enter the word of the day:  ");
            wordOfTheDay = input.nextLine();
            if (!wordOfTheDay.equals("tired")) --attempt;

        }

    } while (attempt >= 1);

    if (attempt == 0) {

        System.out.print("Sorry!  You have no more attempts left!");
    }

    System.exit(0);

}

When you get a correct result, you need to get out of the loop. this is done using the break statement:

if( answer == 24)
{
    System.out.printf( "Please enter your first name, last name, and phone number (no dashes or spaces)\n" +"in a drawing for an all-expenses-paid vacation in the Bahamas: " );
    firstName = input.next();
    lastName = input.next();
    phoneNumber = input.nextLong();
    System.out.printf( "Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,                                + phoneNumber);
    break;
    // that's the break statement
}

this code is incomplete, but it has the ide that you needed,. have a try,.

boolean firstTry = true; boolean keepLooping = true;

do
    {
    if(!firstTry){
            System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
    }

        System.out.printf("Enter the word of the day:  ");
        wordOfTheDay = input.nextLine();
    if(wordOfTheDay.equals("hey!")){
        keepLooping = false;
    }
    --attempt;
    } while (attempt >= 1 && keepLooping);

You'd better user a break to jump out of the loop.Otherwise the loop will continue,print the "3*8=" and wait for your input. If you don't wan't use a break, make the attemp = -1 is also make sense.

if( answer == 24)
         {
            System.out.printf( "Please enter your first name, last name, and phone number (no dashes or spaces)\n"                              +"in a drawing for an all-expenses-paid vacation in the Bahamas: " );

        firstName = input.next();
        lastName = input.next();
        phoneNumber = input.nextLong();
        attempt = -1;
        System.out.printf( "Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,                                + phoneNumber);

         }

Well, obviously you have to break the outer loop.

When you have correct answer (i.e. 'answer == 24') in the if block set some boolean variable 'haveAnswer' to 'true' (which is initialized to 'false' and check in the outer loop before the 'System.out.print(" 3 * 8 = ");' for 'if (haveAnswer) {break;}'

 enter code here`public static void main(String[] args)
 {

 Scanner input = new Scanner(System.in);

int attempt = 2;
int answer = 24;
long phoneNumber = 0;
String firstName = "";
String lastName = "";
String wordOfTheDay = "";


System.out.printf("Enter the word of the day:  ");
wordOfTheDay = input.nextLine();

if(wordOfTheDay.equals("tired"))
{
    boolean haveAnswer = false;
    for( attempt = 2; attempt >= 0; --attempt)
      {
        if (haveAnswer)
            break;
        System.out.print(" 3 * 8 = ");
        answer = input.nextInt();
        input.nextLine();

      if( answer == 24)
         {
            System.out.printf( "Please enter your first name, last name, and phone number (no dashes or spaces)\n"                              +"in a drawing for an all-expenses-paid vacation in the Bahamas: " );

        firstName = input.next();
        lastName = input.next();
        phoneNumber = input.nextLong();

        System.out.printf( "Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,                                + phoneNumber);

          haveAnswer = true;
          break;
         }

        else if( answer != 24)
        {

            if(attempt!=0)
            {

                System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
                    continue;
            }
            else
            {

                System.out.print( "Sorry!  You have no more attempts left!" );
            }
       }

     }

}
else
{
do
{

    System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
    --attempt;
    System.out.printf("Enter the word of the day:  ");
    wordOfTheDay = input.nextLine();
} while (attempt >= 1);


  if( attempt == 0)
  {

     System.out.print( "Sorry!  You have no more attempts left!" );
  }
}

System.exit(0);

}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top