Results 1 to 7 of 7
Thread: Simple Problem with Strings
- 02-05-2012, 04:37 PM #1
Simple Problem with Strings
Again, I have another simple problem, thank goodness they're only simple, these days.
I'm working on a hangman game, sort of. And let's say the secret word is APPLES. How can I display the word to be '------'? I know I need a for loop, but I don't know how to format it. I've tried this;
What I'm thinking is that in the loop, the letter at index 'i' is replaced with a '-', and then it prints the letter, which should now be '-'... But instead, it does nothing and prints the letter that was assigned to it. So as an outprint, I'd get APPLES instead of ------.Java Code:String word = "APPLES" for (int i = 0; i < word.length(); i++ { word.replace(test.charAt(i), '-'); System.out.print(word.charAt(i)); }
Obviously there's something I'm doing wrong, is there any solution or correction I need to make on a specific line?
I don't want to include word = word.replace(word.charAt(i), '-'); within the for loop, because then when trying to guess a letter, it will not recognize the letters in the word, it will only recognize the hyphens.Last edited by CuppaCoffee; 02-05-2012 at 04:41 PM.
-
Re: Simple Problem with Strings
You need to understand a basic fact about Strings -- they are immutable -- they can't be changed at all. So when you call word.replace(...) you are not changing the work String at all, but instead the method creates a new String and returns it. To solve you issue, you must assign this new String object to the word variable:
word = word.replace(test.charAt(i), '-');
-
Re: Simple Problem with Strings
- 02-05-2012, 08:01 PM #4
Re: Simple Problem with Strings
Yeah, I edited my post right after making the thread because I came to the solution that I'll have to use that method. And in what form can I use another String/StringBuffer to hold the hyphens? I'm not used to such strategies when it comes to Strings.
Could it be having two Strings, string1 and string2.. string1 = APPLES, and string 2 would equal the length of string1, except all of its characters replaced with hyphens? Then when I want to test if the letter I picked is in the word, I will compare the letter to string1? If that's so, and I want the letter to be shown (Let's say I choose 'P', for example), how could I get the displayed string to show '-PP---'? Because If that didn't work for the previous method I used, it probably will not work for another plain string, right?
-
Re: Simple Problem with Strings
I would consider using a StringBuffer to hold your dashes and a String to hold the solution String. Then after you test to see if the char is in the String (and has not already been chosen yet), you can use a for loop to iterate through the String, and if the char matches use that index to help you change the char held in that position in the StringBuffer.
- 02-05-2012, 08:29 PM #6
Re: Simple Problem with Strings
I'll try that idea out. Thanks.
Forgive my ignorance though, but I'd like to comprehend this fully; what's the difference between a String and a StringBuffer?
-
Re: Simple Problem with Strings
From what I know, the main differences include
- They are completely different object types and are not interchangeable.
- a StringBuffer is mutable -- the characters in it can be changed without having to create a new StringBuffer object.
- a String is immutable -- once you create a String object, it cannot be changed (ignoring reflection voodoo). If you change a String, you always are in fact creating a new String object or are returning a String that's different from the original String but that's in the String pool.
- To extract the String out of the StringBuffer you call toString() on it.
Similar Threads
-
Simple program, simple problem
By taymilll in forum New To JavaReplies: 12Last Post: 06-20-2011, 05:12 AM -
Simple question involving Strings and JOptionPane
By Napkins in forum New To JavaReplies: 6Last Post: 02-02-2011, 02:30 AM -
Strings Problem
By Freakzoyd in forum New To JavaReplies: 11Last Post: 11-30-2010, 02:45 AM -
[SOLVED] problem with strings
By sandeepsai39 in forum New To JavaReplies: 11Last Post: 02-23-2009, 04:59 PM -
Problem Comparing Strings (its not what you think)
By hilather in forum New To JavaReplies: 7Last Post: 11-19-2008, 06:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks