Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default hangman bluej error

    hello,
    i'm doing this hangman project for quite some time now and i'm stuck at one place,
    Java Code:
    import java.util.*;
    class Hangman
    {
        int choice,z=0;
        public String word="",wrd1 = "",word1="";
        
    
        void menu()
        {
            {
                Scanner scr = new Scanner (System.in);
    
               
                System.out.println("type your choice ");
                choice=scr.nextInt();
                switch(choice)
                {
                    case 1:
                    System.out.println(" \f");
                    game();
                    break;
                    case 2:
                    System.out.println(" \f");
                    instruction();
                    break;
                    case 3:
                    System.out.println(" \f");
                    System.out.println(" Goodbye");
                    System.exit(0);
                    break;
                    default:
                    menu();
                }
            }
        }
    
        void one()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      ");
            System.out.println("|      ");
            System.out.println("|       ");
            System.out.println("|      ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void two()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      ");
            System.out.println("|       ");
            System.out.println("|      ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void three()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|        ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void four()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|       |");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void five()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void six()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|        ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void seven()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|      /");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
    
        void game()
        {
            String word="";
            z=0;
    
            char topic;
            Scanner scr = new Scanner (System.in);
            String a[]={"sarcastic","hallucination ","bowels","dogmass","effluent",};
            String b[]={"die/hard","dude/where/is/my/car","inception","fight/club","shawshank/redemption",};
            String c[]={"uzbekistan","poland","ecuador","djibouti","liechtenstein",};
            String d[]={"hurling","curling","petola","Quidditch","kabaddi",};
            String e[]={" funambulist"," bruit","vivacious","embonpoint","wundtism",};
            int r=0;
            r=(int)(Math.random()*5);
            System.out.println(" what topic would you like to play on");
            System.out.println(" a: Random english words");
            System.out.println(" b: Movies");
            System.out.println(" c: Countries");
            System.out.println(" d: Sports");
            System.out.println(" e: Mystery words");
            topic=scr.next().charAt(0);
            System.out.println(" \f");
            if(topic=='a')
            { word= a[r];
    
            }
            else if(topic=='b')
            {
                word= b[r];
    
            }
            else if(topic=='c')
            {word= c[r];
    
            }
            else if(topic=='d')
            {word= d[r];
    
            }
            else if(topic=='e')
            {word= e[r];
    
            }
            else
                game();
            int len=0;
            len=(word.length());
            char blank[]=new char[len];
            word1=word;
            
            
    
            for(int k=0;k<len;k++)
            {
                blank[k]='_';
            }
            for(int i=0;i==i;i++)
            {
                System.out.println(" \f");
                for(int l=0;l<len;l++)
                {
                 if((word.charAt(l))=='/')
                     blank[l]='/';
                System.out.print(blank[l]+" ");
                
                
                    
                }
    
                if(z==0)
    
                    one();
                else if(z==1)
                    two();
                else if(z==2)
                    three();
                else if(z==3)
                    four();
                else if(z==4)
                    five();
                else if(z==5)
                    six();
                else if(z==6)
                    seven();
    
                System.out.println(" enter your choice"); 
                char guess=scr.next().charAt(0);
                        
                boolean found=false;
                for(int j=0;j<len;j++)
                {
                    if(guess==word.charAt(j))
                    {
                        found=true;
                        
                        blank[j]=guess;
    
                        if(word.indexOf(guess)== word.lastIndexOf(guess))
                           {
                               System.out.println(" letter found");
                               break;
                            }
                       
                    }
                    
                }
                if(found==false)
                {  z=z+1;
                   System.out.println(" sorry, letter not found");
                }
                 
                if(z==7)
                {
                    lose();
                }
                else
                {
                    wrd1 = "";
                    for(int v=0;v<=len-1;v++)
                        wrd1=(wrd1+blank[v]);
    
                }
                if(word.equals(wrd1))
                {
                    win();
                }
                 
            }
        }
        void lose ()
        {
    
            System.out.println(" \f");
            Scanner scr = new Scanner (System.in);
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|      // ");
            System.out.println("|");
            System.out.println("_|___");
    
            System.out.println("you lose");
     System.out.println("The word was "+word1);
            System.out.println("try again? Y/N");
            String choicee=scr.nextLine();
            String choice2=choicee.toUpperCase();
            if(choice2.equals("Y")||choice2.equals("YES")) 
            {
    
                System.out.println(" \f");
                menu();}
            else
            {
                System.out.println(" \f");
                System.out.println(" Goodbye");
                System.exit(0);
    
            }
        }
    
        void win ()
        {
            System.out.println(" \f");
            Scanner scr = new Scanner (System.in);
            System.out.println("congratulations, you win");
            System.out.println("you saved the hangman");
    
            System.out.println("Play again? Y/N");
            String choicee=scr.nextLine();
            String choice2=choicee.toUpperCase();
            if(choice2.equals("Y")||choice2.equals("YES")) 
            {
                System.out.println(" \f");
                menu();}
            else
            {
                System.out.println(" \f");
                System.out.println(" Goodbye");
                System.exit(0);
    
            }
        }
    
        
    }
    in the linear search part( where i am searching for the letter in the word) i have included a letter found statement which for some reason does not print. it has baffled me for quite sometime now and I dont know what to do. if you could go through the program and tell me I would be grateful.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    a letter found statement which for some reason does not print.
    How are you debugging the code? Try adding some println() statements that print out the values of the variables that control whether what you want printed is printed or not. The print out will show you what the computer sees when the code is executed.

    Is there a driver class with a main() method for executing the code for testing?
    Last edited by Norm; 11-16-2014 at 03:50 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    it turns out that after the statement in which i ask for the letter it does not print anything even though theres no clear statement
    Java Code:
    char guess=scr.next().charAt(0);
    Last edited by Norm; 11-16-2014 at 04:02 PM. Reason: Added / in end tag

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    The code you posted is not supposed to print anything. It only reads.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    it does but in parts, have you run it yet? ( maybe i didnt understand)

  6. #6
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    heres the whole thing including the graphical interface
    Java Code:
    import java.util.*;
    class Hangman
    {
        int choice,z=0;
        public String word="",wrd1 = "",word1="";
        
    
        void menu()
        {
            {
                Scanner scr = new Scanner (System.in);
    
                System.out.println("        (/)");
                System.out.println("       (/)");
                System.out.println("        (/)");
                System.out.println("      (/)");
                System.out.println("        (/)");
                System.out.println("       (/)          WELCOME TO HANGMAN");
                System.out.println("       (/))          type your choice");
                System.out.println("      (/)(/)       1: Play Hangman");
                System.out.println("     (/)'`(/)      2: Instructions");
                System.out.println("    (/)    (/)     3: Exit");
                System.out.println("    (/)    (/)");
                System.out.println("    (/)    (/)");
                System.out.println("    (/)    (/)");
                System.out.println("     (/)  (/)");
                System.out.println("      (/)(/)");
                System.out.println("        '' ");
                System.out.println("");
                System.out.println(" ");
                System.out.print(" ");
                System.out.println("type your choice ");
                choice=scr.nextInt();
                switch(choice)
                {
                    case 1:
                    System.out.println(" \f");
                    game();
                    break;
                    case 2:
                    System.out.println(" \f");
                    instruction();
                    break;
                    case 3:
                    System.out.println(" \f");
                    System.out.println(" Goodbye");
                    System.exit(0);
                    break;
                    default:
                    menu();
                }
            }
        }
    
        void one()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      ");
            System.out.println("|      ");
            System.out.println("|       ");
            System.out.println("|      ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void two()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      ");
            System.out.println("|       ");
            System.out.println("|      ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void three()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|        ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void four()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|       |");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void five()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void six()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|        ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void seven()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|      /");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
    
        void game()
        {
            String word="";
            z=0;
    
            char topic;
            Scanner scr = new Scanner (System.in);
            String a[]={"sarcastic","hallucination ","bowels","dogmass","effluent",};
            String b[]={"die/hard","dude/where/is/my/car","inception","fight/club","shawshank/redemption",};
            String c[]={"uzbekistan","poland","ecuador","djibouti","liechtenstein",};
            String d[]={"hurling","curling","petola","Quidditch","kabaddi",};
            String e[]={" funambulist"," bruit","vivacious","embonpoint","wundtism",};
            int r=0;
            r=(int)(Math.random()*5);
            System.out.println(" what topic would you like to play on");
            System.out.println(" a: Random english words");
            System.out.println(" b: Movies");
            System.out.println(" c: Countries");
            System.out.println(" d: Sports");
            System.out.println(" e: Mystery words");
            topic=scr.next().charAt(0);
            System.out.println(" \f");
            if(topic=='a')
            { word= a[r];
    
            }
            else if(topic=='b')
            {
                word= b[r];
    
            }
            else if(topic=='c')
            {word= c[r];
    
            }
            else if(topic=='d')
            {word= d[r];
    
            }
            else if(topic=='e')
            {word= e[r];
    
            }
            else
                game();
            int len=0;
            len=(word.length());
            char blank[]=new char[len];
            word1=word;
            
            
    
            for(int k=0;k<len;k++)
            {
                blank[k]='_';
            }
            for(int i=0;i==i;i++)
            {
                System.out.println(" \f");
                for(int l=0;l<len;l++)
                {
                 if((word.charAt(l))=='/')
                     blank[l]='/';
                System.out.print(blank[l]+" ");
                
                
                    
                }
    
                if(z==0)
    
                    one();
                else if(z==1)
                    two();
                else if(z==2)
                    three();
                else if(z==3)
                    four();
                else if(z==4)
                    five();
                else if(z==5)
                    six();
                else if(z==6)
                    seven();
    
                System.out.println(" enter your choice"); 
                char guess=scr.next().charAt(0);
                        
                boolean found=false;
                for(int j=0;j<len;j++)
                {
                    if(guess==word.charAt(j))
                    {
                        found=true;
                        
                        blank[j]=guess;
    
                        if(word.indexOf(guess)== word.lastIndexOf(guess))
                           {
                               System.out.println(" letter found");
                               break;
                            }
                       
                    }
                    
                }
                if(found==false)
                {  z=z+1;
                   System.out.println(" sorry, letter not found");
                }
                 
                if(z==7)
                {
                    lose();
                }
                else
                {
                    wrd1 = "";
                    for(int v=0;v<=len-1;v++)
                        wrd1=(wrd1+blank[v]);
    
                }
                if(word.equals(wrd1))
                {
                    win();
                }
                 
            }
        }
        void lose ()
        {
    
            System.out.println(" \f");
            Scanner scr = new Scanner (System.in);
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|      // ");
            System.out.println("|");
            System.out.println("_|___");
    
            System.out.println("you lose");
     System.out.println("The word was "+word1);
            System.out.println("try again? Y/N");
            String choicee=scr.nextLine();
            String choice2=choicee.toUpperCase();
            if(choice2.equals("Y")||choice2.equals("YES")) 
            {
    
                System.out.println(" \f");
                menu();}
            else
            {
                System.out.println(" \f");
                System.out.println(" Goodbye");
                System.exit(0);
    
            }
        }
    
        void win ()
        {
            System.out.println(" \f");
            Scanner scr = new Scanner (System.in);
            System.out.println("congratulations, you win");
            System.out.println("you saved the hangman");
    
            System.out.println("Play again? Y/N");
            String choicee=scr.nextLine();
            String choice2=choicee.toUpperCase();
            if(choice2.equals("Y")||choice2.equals("YES")) 
            {
                System.out.println(" \f");
                menu();}
            else
            {
                System.out.println(" \f");
                System.out.println(" Goodbye");
                System.exit(0);
    
            }
        }
    
        void instruction()
        {
    
            {
                Scanner scr = new Scanner (System.in);
                System.out.println("HOW TO PLAY: Choose which topic you want to play.\n");
                System.out.println(" A random word will be selected and the no   ");
                System.out.println("of letters will be represented with blanks.");
                System.out.println("You have 5 lives, every time you guess wrong, one part  ");
                System.out.println("is added to the hangman. Failure to do so will result in losing the game."); 
                System.out.println("Have fun ");
                System.out.println(" ");
                System.out.println("type 1 to return ");
                System.out.println("type any other number to exit");
                int choice1=scr.nextInt();
                if(choice1==1)
                {
                    System.out.println(" \f");
                    menu();}
                else
                {
                    System.out.println(" \f");
                    System.out.println(" Goodbye");
                    System.exit(0);
    
                }
            }
        }
    }
    Last edited by Norm; 11-16-2014 at 04:52 PM. Reason: Fixed code tags

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    Is there a driver class with a main() method for executing the code for testing?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    Quote Originally Posted by Norm View Post
    Is there a driver class with a main() method for executing the code for testing?
    its game() in this one

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    Can you post a small part of the program's output that shows what you are talking about?
    Add some comments that describe what is wrong with the output and show what the output should be.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    Quote Originally Posted by Norm View Post
    Can you post a small part of the program's output that shows what you are talking about?
    Add some comments that describe what is wrong with the output and show what the output should be.
    if wrong,
    _ _ _ _ _ _ _ _

    _________
    |/ |
    | (_)
    |
    |
    |
    |
    _|___
    enter your choice

    whats needed,
    _ _ _ _ _ _ _ _

    _________
    |/ |
    | (_)
    |
    |
    |
    |
    _|___
    enter your choice

    sorry letter not found

    if right,

    _ _ _ _ _ _ _ a

    _________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice

    whats needed
    _ _ _ _ _ _ _ _a

    _________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice

    letter found

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    Where is the message printed? Are there any conditions that are needed to be true for the print statement to be executed?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    yeah

    if(word.indexOf(guess)== word.lastIndexOf(guess))
    {
    System.out.println(" letter found");
    break;
    }

    }

    }
    if(found==false)
    { z=z+1;
    System.out.println(" sorry, letter not found");
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    What is the purpose of the first if() statement in post#12?

    Hint for testing:
    Either print out the value of word when it is chosen so the rest of the print out can be checked
    Or hard code an assignment to word so that the testing is always with the same value until the bug is found and fixed.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    Quote Originally Posted by Norm View Post
    What is the purpose of the first if() statement in post#12?

    Hint for testing:
    Either print out the value of word when it is chosen so the rest of the print out can be checked
    Or hard code an assignment to word so that the testing is always with the same value until the bug is found and fixed.
    the first if statement checks whether the inputted letter(if there are more than 1 of its kind in the word) is the last.

    for example,

    n in banana,

    it checks the n at 2 position,stores it
    word.indexOf(guess)!= word.lastIndexOf(guess)),
    iteration continues,

    it checks the n at 5 position, stores it
    word.indexOf(guess)== word.lastIndexOf(guess)),
    iteration stops

    asks for next guess

    as such for the program mechanics themselfs, they work perfect ly ( the game) its only that it printing after a certain point in the program doesn't work

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    Is the message you want to print controlled by that if statement?

    Have you tested with different words?
    What words does it fail with?
    What words does it work with?
    Last edited by Norm; 11-17-2014 at 05:15 AM.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    it doesn't work with anything :(

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    it doesn't work with anything
    Did it work here with the word= "cat"
    Running: C:\Program Files\Java\jre1.8.0_25\bin\java.exe -client -cp . Hangman2

    what topic would you like to play on
    a: Random english words
    b: Movies
    c: Countries
    d: Sports
    e: Mystery words
    val=a

    word=cat

    _ _ _

    _________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice
    val=a
    letter found

    _ a _

    _________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice
    val=b
    sorry, letter not found

    _ a _

    _________
    |/ |
    | (_)
    |
    |
    |
    |
    _|___
    enter your choice
    val=c
    letter found

    c a _

    _________
    |/ |
    | (_)
    |
    |
    |
    |
    _|___
    enter your choice
    val=d
    sorry, letter not found

    c a _

    _________
    |/ |
    | (_)
    | |
    |
    |
    |
    _|___
    enter your choice
    val=e
    sorry, letter not found

    c a _

    _________
    |/ |
    | (_)
    | |
    | |
    |
    |
    _|___
    enter your choice
    val=f
    sorry, letter not found

    c a _

    _________
    |/ |
    | (_)
    | /|
    | |
    |
    |
    _|___
    enter your choice
    val=g
    sorry, letter not found

    c a _

    _________
    |/ |
    | (_)
    | /|/
    | |
    |
    |
    _|___
    enter your choice
    val=h
    sorry, letter not found

    c a _

    _________
    |/ |
    | (_)
    | /|/
    | |
    | /
    |
    _|___
    enter your choice
    val=i
    sorry, letter not found

    _________
    |/ |
    | (_)
    | /|/
    | |
    | //
    |
    _|___
    you lose
    The word was cat
    try again? Y/N
    val=j

    Goodbye

    0 error(s)
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    Quote Originally Posted by Norm View Post
    Did it work here with the word= "cat"
    i tried and it still didnt :(

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: hangman bluej error

    i tried and it still didnt
    Please post the program's output to show what you are talking about.
    Try with a short simple word like: abc
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: hangman bluej error

    i found out that if I remove the System.out.println("/f") statement in the section I am printing the blanks the output is,
    ________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice
    c
    letter found
    c _ _

    _________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice
    a
    letter found
    c a _

    _________
    |/ |
    |
    |
    |
    |
    |
    _|___
    enter your choice
    h
    sorry, letter not found
    c a _

    _________
    |/ |
    | (_)
    |
    |
    |
    |
    _|___
    enter your choice

    but i dont what the output like that, i want it section by section after each guess
    heres the edited program. I have put a comment statement where I made the correction

    Java Code:
    import java.util.*;
    class Hangman
    {
        int choice,z=0;
        public String word="",wrd1 = "",word1="";
        
    
        void menu()
        {
            {
                Scanner scr = new Scanner (System.in);
    
                System.out.println("        (/)");
                System.out.println("       (/)");
                System.out.println("        (/)");
                System.out.println("      (/)");
                System.out.println("        (/)");
                System.out.println("       (/)          WELCOME TO HANGMAN");
                System.out.println("       (/))          type your choice");
                System.out.println("      (/)(/)       1: Play Hangman");
                System.out.println("     (/)'`(/)      2: Instructions");
                System.out.println("    (/)    (/)     3: Exit");
                System.out.println("    (/)    (/)");
                System.out.println("    (/)    (/)");
                System.out.println("    (/)    (/)");
                System.out.println("     (/)  (/)");
                System.out.println("      (/)(/)");
                System.out.println("        '' ");
                System.out.println("");
                System.out.println(" ");
                System.out.print(" ");
                System.out.println("type your choice ");
                choice=scr.nextInt();
                switch(choice)
                {
                    case 1:
                    System.out.println(" \f");
                    game();
                    break;
                    case 2:
                    System.out.println(" \f");
                    instruction();
                    break;
                    case 3:
                    System.out.println(" \f");
                    System.out.println(" Goodbye");
                    System.exit(0);
                    break;
                    default:
                    menu();
                }
            }
        }
    
        void one()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      ");
            System.out.println("|      ");
            System.out.println("|       ");
            System.out.println("|      ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void two()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      ");
            System.out.println("|       ");
            System.out.println("|      ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void three()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|        ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void four()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|       |");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void five()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|");
            System.out.println("|       |");
            System.out.println("|       ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void six()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|        ");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
        void seven()
        {
            System.out.println("\n");
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|      /");
            System.out.println("|");
            System.out.println("_|___");
    
        }
    
    
        void game()
        {
            String word="";
            z=0;
    
            char topic;
            Scanner scr = new Scanner (System.in);
            String a[]={"cat","sarcastic","hallucination ","bowels","dogmass","effluent",};
            String b[]={"die/hard","dude/where/is/my/car","inception","fight/club","shawshank/redemption",};
            String c[]={"uzbekistan","poland","ecuador","djibouti","liechtenstein",};
            String d[]={"hurling","curling","petola","Quidditch","kabaddi",};
            String e[]={" funambulist"," bruit","vivacious","embonpoint","wundtism",};
            int r=0;
            r=(int)(Math.random()*5);
            System.out.println(" what topic would you like to play on");
            System.out.println(" a: Random english words");
            System.out.println(" b: Movies");
            System.out.println(" c: Countries");
            System.out.println(" d: Sports");
            System.out.println(" e: Mystery words");
            topic=scr.next().charAt(0);
            System.out.println(" \f");
            if(topic=='a')
            { word= a[0];
    
            }
            else if(topic=='b')
            {
                word= b[r];
    
            }
            else if(topic=='c')
            {word= c[r];
    
            }
            else if(topic=='d')
            {word= d[r];
    
            }
            else if(topic=='e')
            {word= e[r];
    
            }
            else
                game();
            
                int len=0;
            len=(word.length());
            char blank[]=new char[len];
            word1=word;
            
            
    
            for(int k=0;k<len;k++)
            {
                blank[k]='_';
            }
            for(int i=0;i==i;i++)
            {
                // edit...System.out.println(" \f");
                for(int l=0;l<len;l++)
                {
                 if((word.charAt(l))=='/')
                     blank[l]='/';
                System.out.print(blank[l]+" ");
                
                
                    
                }
                 
                if(z==0)
    
                    one();
                else if(z==1)
                    two();
                else if(z==2)
                    three();
                else if(z==3)
                    four();
                else if(z==4)
                    five();
                else if(z==5)
                    six();
                else if(z==6)
                    seven();
    
                System.out.println(" enter your choice"); 
                char guess=scr.next().charAt(0);
                        
                boolean found=false;
                for(int j=0;j<len;j++)
                {
                    if(guess==word.charAt(j))
                    {
                        found=true;
                        
                        blank[j]=guess;
    
                        if(word.indexOf(guess)== word.lastIndexOf(guess))
                           {
                               System.out.println(" letter found");
                               break;
                            }
                       
                    }
                    
                }
                if(found==false)
                {  z=z+1;
                   System.out.println(" sorry, letter not found");
                }
                 
                if(z==7)
                {
                    lose();
                }
                else
                {
                    wrd1 = "";
                    for(int v=0;v<=len-1;v++)
                        wrd1=(wrd1+blank[v]);
    
                }
                if(word.equals(wrd1))
                {
                    win();
                }
                 
            }
        }
        void lose ()
        {
    
            System.out.println(" \f");
            Scanner scr = new Scanner (System.in);
            System.out.println("_________");
            System.out.println("|/      |");
            System.out.println("|      (_)");
            System.out.println("|      /|/");
            System.out.println("|       |");
            System.out.println("|      // ");
            System.out.println("|");
            System.out.println("_|___");
    
            System.out.println("you lose");
     System.out.println("The word was "+word1);
            System.out.println("try again? Y/N");
            String choicee=scr.nextLine();
            String choice2=choicee.toUpperCase();
            if(choice2.equals("Y")||choice2.equals("YES")) 
            {
    
                System.out.println(" \f");
                menu();}
            else
            {
                System.out.println(" \f");
                System.out.println(" Goodbye");
                System.exit(0);
    
            }
        }
    
        void win ()
        {
            System.out.println(" \f");
            Scanner scr = new Scanner (System.in);
            System.out.println("congratulations, you win");
            System.out.println("you saved the hangman");
    
            System.out.println("Play again? Y/N");
            String choicee=scr.nextLine();
            String choice2=choicee.toUpperCase();
            if(choice2.equals("Y")||choice2.equals("YES")) 
            {
                System.out.println(" \f");
                menu();}
            else
            {
                System.out.println(" \f");
                System.out.println(" Goodbye");
                System.exit(0);
    
            }
        }
    
        void instruction()
        {
    
            {
                Scanner scr = new Scanner (System.in);
                System.out.println("HOW TO PLAY: Choose which topic you want to play.\n");
                System.out.println(" A random word will be selected and the no   ");
                System.out.println("of letters will be represented with blanks.");
                System.out.println("You have 5 lives, every time you guess wrong, one part  ");
                System.out.println("is added to the hangman. Failure to do so will result in losing the game."); 
                System.out.println("Have fun ");
                System.out.println(" ");
                System.out.println("type 1 to return ");
                System.out.println("type any other number to exit");
                int choice1=scr.nextInt();
                if(choice1==1)
                {
                    System.out.println(" \f");
                    menu();}
                else
                {
                    System.out.println(" \f");
                    System.out.println(" Goodbye");
                    System.exit(0);
    
                }
            }
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Error in Bluej
    By deyan8 in forum New To Java
    Replies: 4
    Last Post: 08-10-2014, 07:05 PM
  2. BlueJ help! weird error text meesed up
    By linux1man in forum Other IDEs
    Replies: 6
    Last Post: 02-07-2013, 04:54 PM
  3. BlueJ with Modified Error Subsystem
    By java software in forum Java Software
    Replies: 0
    Last Post: 12-19-2011, 11:49 AM
  4. Error Compiling Project in BlueJ
    By java1337 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 09:04 PM
  5. Bluej Hangman Game Help
    By P0CK3Y in forum New To Java
    Replies: 0
    Last Post: 03-29-2011, 11:14 AM

Posting Permissions

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