Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I received this assignment from my professor. I have no idea where to start othe

ID: 3631724 • Letter: I

Question

I received this assignment from my professor. I have no idea where to start other than having a user input.
***************************************************************************
Here is the assign:

There is a "fun" children's game where one child thinks of a "common phrase", then the second child repeatedly makes guesses as to the letters that it contains.

You are to write a Java program that:
1. Prompts a user to enter a "common phrase", and reads it into a variable using Scanner.nextLine() - under the assumption that the phrase consists of nothing but under case letters and spaces.
2. The following is than repeated until the entire "common phrase" is revealed:
The "common phrase" is displayed with all of the letters (that have not yet been correctly guessed) replaced with the ? character and all of the letters that have been correctly guessed displayed as is.
Using a user input validation loop, the second user is prompted to enter a single lowercase letter guess.
**Hints:
Store the letters that have been correctly guessed in an initially empty ("") String object, using the concatenation operator (+=).
A fair amount of partial credit shall be given for just being able to respond with "correct" / "incorrect" for each second user guess.



Sample run(s):
Please enter the phrase to guess at : who do you love



Common Phrase
-------------
??? ?? ??? ????


Enter a lowercase letter guess : f



Common Phrase
-------------
??? ?? ??? ????


Enter a lowercase letter guess : o



Common Phrase
-------------
??o ?o ?o? ?o??


Enter a lowercase letter guess : s



Common Phrase
-------------
??o ?o ?o? ?o??


Enter a lowercase letter guess : w



Common Phrase
-------------
w?o ?o ?o? ?o??


Enter a lowercase letter guess : h



Common Phrase
-------------
who ?o ?o? ?o??


Enter a lowercase letter guess : d



Common Phrase
-------------
who do ?o? ?o??


Enter a lowercase letter guess : e



Common Phrase
-------------
who do ?o? ?o?e


Enter a lowercase letter guess : y



Common Phrase
-------------
who do yo? ?o?e


Enter a lowercase letter guess : u



Common Phrase
-------------
who do you ?o?e


Enter a lowercase letter guess : l



Common Phrase
-------------
who do you lo?e


Enter a lowercase letter guess : v



Common Phrase
-------------
who do you love

How I intend to attack the problem:
1) Have the user input a message, and store this in a string.
2)Have letter counter, set the count equal to "?"
3)Store each guess as a char() and run this all through a loop until the message is solved.

And, that's where I'm at. Help please and thank you.

Explanation / Answer

those steps you described at the bottom are exactly it! all you were missing was a little knowledge about java Strings and how to play around with them. you can read about all the neat functions that come with strings right here (http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html)

Here is code that behaves the way you want it, it evern accepts uppercase phrases and converts them to lowercase before comparing :). The code is fairly straightforward, so run it a couple times, read through it carefully to understand each step, and if anything isn't as it should be - you can PM me for modifications and/or further explanation. Otherwise , please remeber to rate, and good luck :)

import java.util.Scanner;

public class GuessingGame {

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

        System.out.print("Please enter the phrase to guess at: ");
        String commonPhrase = input.nextLine();
        commonPhrase = commonPhrase.toLowerCase();
        String guessed = "";
        for (int i = 0; i < commonPhrase.length(); i++) {
            if (commonPhrase.charAt(i) != ' ') {
                guessed += "?";
            } else {
                guessed += " ";
            }
        }

        do {
            System.out.println(" Common Phrase ------------- " + guessed);
            char aguess = ' ';
            System.out.print("Enter a lowercase letter guess: ");
            aguess = input.nextLine().charAt(0);
            if (commonPhrase.indexOf(aguess) > -1) {
                String prev = guessed;
                guessed = "";
                for (int i = 0; i < commonPhrase.length(); i++) {
                    if (commonPhrase.charAt(i) == aguess) {
                        guessed += aguess;
                    } else {
                        guessed += prev.charAt(i);
                    }
                }
            }
        } while (guessed.compareTo(commonPhrase) != 0);
        System.out.println(" Common Phrase ------------- " + guessed);
    }
}