Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: I don't know how to create this method, it eludes me, please help ASAP !!

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I don't know how to create this method, it eludes me, please help ASAP !!

    Below is part Two the method I need help creating:

    This method updates the sequence of stars according to the actual English word and the guess character.

    public static String updateSecretWord(String secretWord, String englishWord, String guessedChar)
    {

    }
    below is the example of how it should look like when I call it

    updateSecretWord(“*****”, “Hello”, “h”) = h****
    updateSecretWord(“h****”, “Hello”, “o”) = h***o
    updateSecretWord(“he**o”, “Hello”, “e”) = he**o


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Can you describe how you'd do it in plain language? Something like:

    Find location of guessChar in englishWord and change that same location in secretWord from an asterisk to the guessChar.

    Try to program that.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Could you please show me that in code, because I tried doing it, it didn't work =/

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Show the code you tried, describe wha "didn't work," post any errors you want help with, and ask specific questions. We can't do it for you, but we can help you with code you've written.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    So I used this example found on the internet to help me out:
    Example Code:
    String myName = "domanokz";
    myName.charAt(4) = 'x';

    This is what I came up with:

    //UpdateSecretWord Method
     
      public static String updateSecretWord(String secretWord, String englishWord, String guessedChar) 
      { 
        Scanner in = new Scanner(System.in);
        guessedChar = in.next();
        int wordLength = secretWord.length();
        if(englishWord.toLowerCase().contains(guessedChar.toLowerCase()) )
        {
          int index = englishWord.indexOf(guessedChar);
     
          String updateSecretWord = secretWord;
          updateSecretWord.charAt(index) = guessedChar;   <----- Error is in this line says (Error: unexpected type required: variable found:    value)
          System.out.println(updateSecretWord);
        }
        return updateSecretWord;
      }
    Last edited by GregBrannon; January 12th, 2014 at 06:16 AM. Reason: Added code tags. Please use code tags.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    I added code tags to your post. You can edit the post and see what I did so that you can do that in the future OR read this link to get that and other info useful for newcomers.

    Looking at the code you posted:

    There's no reason for a Scanner object in that method. Get rid of it. Get rid of the lines prior to the if() statement.

    That's not how the charAt() method works. Read the String API. While there, look for another String method that might help you.

    And it's interesting you found that example on the Internet HERE, posted by someone who said it wasn't working as designed and then apparently ignored the advice given in the rest of that post. You need to be more careful in using code you find on the Internet.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Okay so this is what I got :

    //UpdateSecretWord Method
      public static String updateSecretWord(String secretWord, String englishWord, String guessedChar) 
      { 
        if(englishWord.toLowerCase().contains(guessedChar.toLowerCase()) )
        {
          int index = englishWord.indexOf(guessedChar); 
          StringBuilder sb = new StringBuilder(secretWord);
          sb.setCharAt(index, guessedChar); <---- the error is here it says: "Required: int, char     Found: int, java.lang.String." 
          System.out.println(sb);     
        }
        else
        {
          System.out.println("The guess you have entered does not match any letter in the word");
        }
        return secretWord;
      }

    I'm pretty sure the variable guessedChar is the problem. It is a one letter string input from the user, if it helps you just so you know this program I'm doing is for a text based hangman game. How could I change it into a Char ?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Yes, the StringBuilder.setCharAt() method requires an ( int, char ) as parameters - you can read this yourself in the API. In your method, guessedChar is passed as a String, so you need to either:

    1. pass guessedChar as a char
    2. convert the String guessedChar to a char in the method
    3. pass a different parameter to setCharAt(), like englishWord.charAt( index )

    Option 3 might be considered more elegant by some, because it preserve's the original word's capitalization.

    Our goal is to enable you to determine these options and corresponding solutions yourself, and that starts with practice for confidence and greater familiarity with the SE API, all your responsibility.

    Keep coding!

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    Artus (January 12th, 2014)

  10. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Thank you ! I turned it into a char, because I need it to not keep the capitalization anyway. however I encountered another problem, if I enter the guessChar as the first letter of the said secretWord, then it says "String index out of range: -1." It works now though if I enter any other letter of said secretWord.

    How could I fix this ?

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    The result -1 is returned when the character sought isn't found. Why might that be happening, especially on the first letter? You can fix that yourself.

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    Artus (January 12th, 2014)

  13. #11
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't know how to create this method, it eludes me, please help ASAP !!

    Okay thanks a bunch! Gonna sleep for now so tired.

Similar Threads

  1. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  2. [SOLVED] I don't know why this error using get() method from the ArrayList class
    By juanp_1982 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2012, 03:11 PM
  3. Help ! merge this code into 1 main method . ASAP :( tnx
    By anitsirc in forum Object Oriented Programming
    Replies: 2
    Last Post: November 23rd, 2011, 11:53 AM
  4. About add and remove method of ListNode, i don't understand....
    By Timloneungpo in forum Collections and Generics
    Replies: 1
    Last Post: September 28th, 2011, 08:09 AM
  5. Replies: 16
    Last Post: September 11th, 2011, 07:33 AM