View Full Version : JAVA replace method
the_usualsuper
11-25-2006, 09:12 PM
Hi, I am writing a java code for a simple version of hangman and I need help with replacing the word I am starting off with into asterisks without using a while method.
So for example:
enter a word-> hello
*****
so how can I go from "hello" to "*****" using a replace method?
I don't quite understand your question, but I'll give some advice.
String word = hello;
for(int i = 0; i < word.length; i++)
{
word.replace(word[i], "*");
}
If my code above is syntactiaclly correct, that will replace all letters in "word" with *. But since you've changed "hello" to "*****" you've lost the secret word completely. Maybe you want something like...
String word = hello;
String coverUpWord = '';
for(int i = 0; i < word.length; i++)
{
coverUpWord += '*';
}
Now both the above use for-loops not while-loops, but they are essentially the same. Why is it that you can't use a while-loop?
the_usualsuper
11-26-2006, 01:04 AM
Well, I'm trying to do it without any loops at all. I'm trying to get it so if a person enters a letter to guess what the word is, then it would be a lot easier to replace the ***** with a letter if they guess correctly. Like if I use "hello" again, and guess the letter e, then the ***** would change to *e***.
the_usualsuper
11-26-2006, 01:21 AM
Okay, sorry to bother again, but I'm also having another problem with my code. If I enter a letter to the covered up *****, and the letter is correct, then the enter line of ***** becomes the letter I picked.
Example:
enter a word-> hello
*****
enter a letter-> h
hhhhh
What should I do? I want the program just to print out h**** but it prints out hhhhh. Is there something wrong with my replace() method?
brad211987
11-26-2006, 04:48 AM
Post your code on the forum so we can see what is causing the problem. There is no telling why it is changing ***** to hhhhh without seeing the code, all we can do is guess.
the_usualsuper
11-26-2006, 04:50 AM
public class Hangman {
private String input, input2;
private char letter, letterlocate;
private int count, lengthi, index, location;
public Hangman() {
input="";
input2="";
letter='a';
count=0;
lengthi=0;
location=0;
index=0;
letterlocate='a';
}
public static void main (String[]args) {
Hangman game = new Hangman();
game.Play();
}
public void Play() {
StartGame();
PlayGame();
EndGame();
}
public void StartGame(){ // enter a word
TextReader console = new TextReader();
System.out.println("WELCOME TO HANGMAN!");
System.out.print("\nEnter a word -> ");
input = console.readLine();
input = input.toLowerCase();
System.out.println("\n");
lengthi = input.length();
for(index = 0; index < lengthi; index++)
{
input2 += "*";
}
System.out.println(input2);
System.out.println("\n");
System.out.println("Wrong Choices: 0");
}
public void PlayGame(){
TextReader console = new TextReader();
for (count=0; count <= 7; count++) {
System.out.print("Enter a letter: ");
letter = console.readChar();
System.out.println("\n");
location = input.indexOf(letter);
if (location < 0 || location > lengthi - 1 ) {
count = count + 1;
System.out.println(input2);
}
else {
letterlocate = input2.charAt(location);
String newinput= input2.replace(letterlocate, letter);
System.out.println(newinput);
}
if (input2.equals(input))
count = 8;
}
}
public void EndGame() {
if (count == 7) {
System.out.println("\n\nOh NOOOO! You Have Lost!");
System.out.println("The word was: " + input);
}
if (count == 8) {
System.out.println("Yay!!! You Have Won!");
System.out.println("The word was: " + input);
}
}
}
The main problem is located here:
public void PlayGame(){
TextReader console = new TextReader();
for (count=0; count <= 7; count++) {
System.out.print("Enter a letter: ");
letter = console.readChar();
System.out.println("\n");
location = input.indexOf(letter);
if (location < 0 || location > lengthi - 1 ) {
count = count + 1;
System.out.println(input2);
}
else {
letterlocate = input2.charAt(location);
String newinput= input2.replace(letterlocate, letter);
System.out.println(newinput);
}
if (input2.equals(input))
count = 8;
}
Aradon
11-26-2006, 05:45 AM
As per the Java API on replace:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
SO, when you have input2, which holds ***** (aka hello).
So you enter the H. H is indeed in hello. So you get the character that is at the word and grab that character that is in input2, which is a *. You then replace all *s with an h.
Personally I would consider doing this through two array's of characters. The actual word and the word the user see's.
That way you can have an array that holds
char[] userWord = {'*', '*', '*', '*', '*'};
and one that holds
char[] actWord = {'h', 'e', 'l', 'l', 'o'};
So then all you have to do is find out which index the letter is in, then replace the same index in the userWord. This would also allow for easy printing since all you have to do is print out the userWord.
Is it the most efficent? No. But, it is indeed the best way to show off your skills in for loops as well as providing an easy way to keep track of characters.
If you really wanted to continue to use string operations however you would have to look into replaceFirst (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceFirst(java.lang.String,%20java.lang.String)) and Pattern (http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html) for that.
Oh, and my guess is the reason he can't use a while loop is because it's an assignment, but that's okay, cause at least he's trying ;)
the_usualsuper
11-26-2006, 05:50 PM
Oh no. The reason I don't want to use while loops is because I've tried coding with while loops but the thing keeps screwing up. The code is:
while(index < lengthofinput)
{
System.out.print("*");
index++;
}
I just can't get the line of * to equal to a String.
Aradon
11-26-2006, 06:36 PM
Oh no. The reason I don't want to use while loops is because I've tried coding with while loops but the thing keeps screwing up. The code is:
while(index < lengthofinput)
{
System.out.print("*");
index++;
}
I just can't get the line of * to equal to a String.
Well that depends upon what index is and what lengthofinput is. So something like..
int index = 0;
String hello = "hello";
int lengthofinput = hello.length();
while(index < lengthofinput)
{
System.out.print("*");
index++;
}
will produce
*****
Of course if you start at 1 for index that will produce four *'s instead of 5 *'s, and so on and so on.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.