Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Strings, if statements, while loops

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strings, if statements, while loops

    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 th assumption that the phrase consists of nothing but undercase 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 (+=).
    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


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings, if statements, while loops

    This is what I have on eclipse now:

    import java.util.Scanner;

    public class cs201
    {
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    String guess;
    String commonPhrase = "who do you love";
    Scanner.nextLine();
    System.out.print("Please enter the phrase to guess at : who do you love");
    }
    }
    obviously i'm not very far at all. I just need some direction because we haven't had assignments like this one before

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings, if statements, while loops

    Professor gave us a few lines of code that should be used in this program so I put it all together just to have it all there. It's certainly not in order towards the end but I don't know how exactly to use this all for the program. this is what I have on my screen now:


    import java.util.Scanner;


    /******************************
    * Take Home Exam
    * Matt Taylor
    *
    * Guessing that phrase that pays the A
    ********************************/
    public class TakeHomeExam
    {
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    String guess= "";
    System.out.print("Please enter the phrase to guess at : ");
    String commonPhrase= stdIn.nextInt();

    for (int i=0; i < commonPhrase.length(); ++i);
    System.out.print (commonPhrase.chartAt(i));

    commonPhrase.charAt(i)==
    guess.charAt(0)
    }
    }

Similar Threads

  1. Using loops and control statements to draw lines
    By john.adam in forum Object Oriented Programming
    Replies: 3
    Last Post: January 19th, 2012, 09:51 AM
  2. [SOLVED] Nested Loops & Processing Strings
    By Usoda in forum What's Wrong With My Code?
    Replies: 20
    Last Post: October 20th, 2011, 07:15 PM
  3. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  4. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM