4 Replies - 1749 Views - Last Post: 27 October 2011 - 09:13 PM Rate Topic: -----

#1 collegejavanoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 11

Word guessing game -- replacing chars

Posted 27 October 2011 - 08:43 PM

I am creating a word guessing game where I have to use for/while loops to get user input to match their guesses to the secret word while putting the guessed letters into a different string. I have searched for this but either people are doing a more complex hangman game or they have more methods and types than my class has learned so far.

So my question is, how do I go about making characters combine with the question marks to show progress? Say the person guessed the letter 'l' in the code below. How do I make the progress line output "??ll? ???l?" ? Also, we are not to ACTUALLY replace ?'s with the correctly guessed letter with some code we haven't learned about yet. I just have no idea how to make the program check the secret word for correct user-input letters.


Here's the code so far:

import java.util.Scanner;
public class wordguess {
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		
			{
				String answer = ("hello world");   // a pre-set word, will be user-input later
			
				String unfinished = ("????? ?????");   // ?'s used to hide the word
				String progress = ("");                 // add characters to ?'s to show progress
				String wrong = ("");                    // display guessed incorrect letters

				System.out.print("Enter a letter to find out what " + unfinished + " is: ");
				String try1 = in.next();                // store first try, unneeded??
				
				if (try1.length() != 1)                  // make sure it is one letter
					{
					System.out.println("Try again, enter one letter: ");
					try1 = in.next();
					}
				
				do                                     // for later
				{
					
					
					
				}
		
				
				
				while(answer!=progress);
	}
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Word guessing game -- replacing chars

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: Word guessing game -- replacing chars

Posted 27 October 2011 - 08:44 PM

You should really use a StringBuilder here, as StringBuilder is mutable and String is not.

http://download.orac...ingBuilder.html
Was This Post Helpful? 0
  • +
  • -

#3 collegejavanoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 11

Re: Word guessing game -- replacing chars

Posted 27 October 2011 - 09:06 PM

I heard about this StringBuilder, however we have not learned about that either and are most likely not supposed to use it. But if the time comes.. I shall..
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: Word guessing game -- replacing chars

Posted 27 October 2011 - 09:08 PM

If you aren't allowed to use it, then store a char[] and update that. That's the next best option. Remember that a String class constructor accepts a char[] as a parameter.
Was This Post Helpful? 1
  • +
  • -

#5 collegejavanoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 11

Re: Word guessing game -- replacing chars

Posted 27 October 2011 - 09:13 PM

Ahh yes. How could I forget about the simple char[]?! Thank you. I shall be back if I run into more problems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1