Changing Certain Chars in A String
Question
I'm a beginner programmer and I'm trying to make a very simple version of Hangman. I'm having so problem with my code. This is the portion of code I'm having a problem with:
for(int i = 0; i <= wordLength - 1; i++ ) {
if (letter == theWord.charAt( i )) {
onemore=i+1;
System.out.println("This letter matches with letter number " + onemore + " in the word.");
***displayWord.charAt(i)=letter;***
System.out.println("The word so far is " + displayWord);
}
}
The part I'm getting an error with has 3 asterisks on either side of it.
displayWord is a String and letter is a char.
Netbeans tells me:
unexpected type
required: variable
found: value
I have no idea what the problem is.
La solution
Basically, Java String
s are immutable, that is, there contents can't be changed.
You'd be better of using a StringBuilder
.
String theWord = "This is a simple test";
char letter = 'i';
char changeTo = '-';
StringBuilder displayWord = new StringBuilder(theWord);
int i = theWord.indexOf(letter);
if (i != -1) {
System.out.println("This letter matches with letter number " + (i + 1) + " in the word.");
displayWord.setCharAt(i, changeTo);
System.out.println("The word so far is " + displayWord);
}
System.out.println(displayWord);
This results in:
This letter matches with letter number 3 in the word.
The word so far is Th-s is a simple test
This letter matches with letter number 6 in the word.
The word so far is Th-s -s a simple test
This letter matches with letter number 12 in the word.
The word so far is Th-s -s a s-mple test
Th-s -s a s-mple test
Now the short version might look something like
String displayWord = theWord.repace(letter, changeTo);
Autres conseils
charAt(i) RETURNS the value of the letter at that part of the word, you can't use it to CHANGE the value. Strings in Java are immutable, so you actually can't change this one. You can create a new one (using a constructor or possible one of the replaceXXX functions) and assign it back to displayWord, or take a look at the StringBuffer class which is more efficient for this type of thing.
Since String
is an immutable object, you can't change its content. Immutable means that once the content of the String
is set, you don't have the ability of changing it in any way. You can always change the reference but never the content itself.
That being said, the String
object doesn't offer any method to violate this principle. There's no setCharAt
and you can't assign a new value to the result of charAt(i)
. Note how some methods that "seem" to change the String
(like replace
) are instead returning a new cloned object with changes on it. For example:
String sample = "Hello World!";
sample.replace('e', '*');
System.out.println(sample);
The above code is not making any changes on sample
, so the result printed on the screen will be Hello World!
. In order to capture the changes made by the replace
method, we need to reassign the sample
variable to the result of replace
:
String sample = "Hello World!";
sample = sample.replace('e', '*');
System.out.println(sample);
In this case we are updating our sample
variable with the new cloned String
returned by replace
. This value will have the changes, so we are going to see H*llo World!
printed out.
Now that you know why you can't change the content of your String
s, let's see what solutions you have.
First and foremost, to keep the exact behavior of your program, the perfect way to go is using a
StringBuilder
like @MadProgrammer pointed out before. An over simplistic way of looking at theStringBuilder
class is basically as a mutableString
. With it you can do all kind of replacements and modifications.Another solution (and not recommended) is to play with the
String
to create a new copy every time you want to change a character. This is going to be really cumbersome and offers no advantages over theStringBuilder
solution, so I'm not going to go over the details.The third solution depends on your requirements and how flexible you can be. If all that you want to do is to replace every occurrence of a certain character in your
String
, then your code can be simplified a lot and you can use thereplace
method I talked about before. It will be something like:String theWord = "Here is our testing string"; char letter = 'e'; char changeTo = '*'; String displayWord = theWord.replace(letter, changeTo); System.out.println("The resulting word is: " + displayWord);
This will output the following result:
The resulting word is: H*r* is our t*sting string
Again, this only works in case your requirements are flexible enough and you can skip the "step by step" approach you are following in your example code.
I'm not sure about that error, but in Java you can't update the character in a style like that.
Instead, try using StringBuilder
for your displayWord. That has a method: setCharAt
that might be what you want.
Good luck coding :)