Results 1 to 8 of 8
  1. #1
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    23
    Rep Power
    0

    Default Boolean not getting result from method?

    I'm to code a game of Hangman, however, when I call check() method, the method doesn't appear to return true, even though the guess letter is correct. can someone help me understand why this is?
    Java Code:
    package hangover;
    import javax.swing.*;
    /**
     * Name: Ben Hughes
     * Date: Thursday, October 15th, 2015
     * Purpose: To play the game of hangman
     */
    public class Hangover {
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null, "Welcome to the game of Hangman!\n\nThe category the word is from will be 'Chocolate Bars'");
            
            String choices[] = {"CRUNCHIE", "SKOR", "KIT KAT", "TWIX", "HERSHEY", "CRISPY CRUNCH", "OH HENRY", "SNICKERS", "MILKY BAR", "MARS"};
            String hangman[][] = {{" ", "O", " "}, {"/", "|", "\\"}, {" ", "|", " "},{"/", " ", "\\"}};
            int rng = rng();
            boolean check;
            String word = choices[rng];
            String blank = "";
            
            for(int i = 0; i < word.length(); i++){ //Creates the string to display the blank spaces of each letter
                if(word.charAt(i) == 32){
                    blank = blank + "\t";
                }
                else{
                    blank = blank + "-";
                }
            }
            
            for(int i = 0; i < 4; i++){ //This is to print the hangman
                for(int a = 0; a < 3; a++){
                    System.out.print(hangman[i][a]);
                }
                System.out.print("\n");
            }
            
            System.out.println(blank);
            
            for(int guesses = 7; guesses >= 0; guesses--){ //If they run out of guesses, close the program
                if(guesses == 0){
                    JOptionPane.showMessageDialog(null, "You lost! The word was: " + word);
                    System.exit(0);
                }
                else{
                    String input = JOptionPane.showInputDialog("Number of guesses remaining: " + guesses + "\n\nPlease enter a letter");
                    char letter = input.charAt(0); //Converts the inputted guess to a char to be used
    
                    check = check(word, letter);
                    
                    if(check == true){ //If the word has the letter anywhere, find where it is and replace it
                        for(int i = 0; i < word.length(); i++){
                            if(letter == word.charAt(i)){
                                char[] blankArray = blank.toCharArray();
                                blankArray[i] = letter;
                                blank = String.valueOf(blankArray);
                                System.out.println(blank);
                            }                  
                        }
                    }
                }
            }
        }
        
        public static boolean check(String word, char letter){
            boolean check = false;
            for(int i = 0; i < word.length(); i++){
                if(check == false){
                    if(letter == word.charAt(i)){
                        check = true;
                    }
                    else{
                        check = false;
                    }
                }
            }
            return check;
        }    
        
        public static void printHangman(String word, char letter){
        
        }
        
        public static int rng(){
            double rngDec = Math.random() * 9 + 1;
            int rng = (int)Math.round(rngDec);
            return rng;    
        }  
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Boolean not getting result from method?

    This is your loop:
    Java Code:
            for(int i = 0; i < word.length(); i++){
                if(check == false){
                    if(letter == word.charAt(i)){
                        check = true;
                    }
                    else{
                        check = false;
                    }
                }
            }
    Let's step through this for the word "blah" and the letter "a".

    First time through we compare 'a' with 'b', no match so 'check' is still false.
    Next time round we compare 'a' with 'l'...still no match...still false.
    Third time we compare 'a' with 'a'...yay...match...check is now true.
    And then we go round the loop again...and compare 'a' with 'h'...no match...check is no false again.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    23
    Rep Power
    0

    Default Re: Boolean not getting result from method?

    But in that instance where check is true... it is supposed to check the boolean before checking the letter. What I mean is, the word.charAt() is only supposed to run when check is false. When it finally finds a, in blah, it changes check to true and therefore the second if statement should not run. So if that's the case, why is it still running, and is there anyway to fix this?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Boolean not getting result from method?

    Your check() method is okay (but not very efficient). However, are you considering the case of the letter you enter?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Boolean not getting result from method?

    A couple of other observations.

    1. You will never pick a "crunchie."
    2. Don't use magic numbers (e.g. the number 9 in the rng method). What happens if you add more words?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Boolean not getting result from method?

    Quote Originally Posted by madtaffy View Post
    But in that instance where check is true... it is supposed to check the boolean before checking the letter. What I mean is, the word.charAt() is only supposed to run when check is false. When it finally finds a, in blah, it changes check to true and therefore the second if statement should not run. So if that's the case, why is it still running, and is there anyway to fix this?
    You can always exit the loop. There are multiple ways ( return true, break, etc.). But what happens if you have a word that carries multiple "a"? I have to admit that I am not too familiar with "hangman", but shouldn' t the game show all letters of the chosen kind?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Boolean not getting result from method?

    Quote Originally Posted by madtaffy View Post
    But in that instance where check is true...
    Ha...never answer a question at the end of a long day...:)

    Completely missed the if (!check) bit!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Boolean not getting result from method?

    Quote Originally Posted by jim829 View Post
    You will never pick a "crunchie."
    Good; I don't like crunchies ...

    kindest regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 6
    Last Post: 01-16-2014, 01:48 PM
  2. Confused about the result of method
    By JayCut in forum New To Java
    Replies: 19
    Last Post: 11-01-2013, 04:29 PM
  3. Boolean method help
    By Zigster in forum New To Java
    Replies: 15
    Last Post: 04-24-2012, 10:52 PM
  4. Problems with method which returns result
    By new Object() in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 12:16 PM
  5. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 01:32 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
  •