Results 1 to 7 of 7
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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;

    Java Code:
    String word = "APPLES"
    
    for (int i = 0; i < word.length(); i++
    {
    word.replace(test.charAt(i), '-');
    System.out.print(word.charAt(i));
    }
    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 ------.

    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 05:41 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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), '-');

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple Problem with Strings

    Quote Originally Posted by CuppaCoffee View Post
    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.
    I did not see this in your original post -- did you edit it?

    Well the only solution then is to use another String or StringBuffer to hold the hyphens, no?

  4. #4
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  2. Simple question involving Strings and JOptionPane
    By Napkins in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 03:30 AM
  3. Strings Problem
    By Freakzoyd in forum New To Java
    Replies: 11
    Last Post: 11-30-2010, 03:45 AM
  4. [SOLVED] problem with strings
    By sandeepsai39 in forum New To Java
    Replies: 11
    Last Post: 02-23-2009, 05:59 PM
  5. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 07:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •