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 5 of 5

Thread: Java Hangman program! Need help reading an ar

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Hangman program! Need help reading an ar

    This program runs but does not execute this part of the code how I want it to. When I take out what is posted below the program works fine but when I leave it in the program ends after the first letter is entered..
    I want the array guesshold to hold the guesses from the user and in this part i have posted below compare the input with everything else that has been entered to see if it has been previously entered. if you want to give any other input on other parts please feel free i am still a beginner!

    /*
    for(int counter = 0; counter <= 10; counter++)
    {
    if(guesshold[counter +1 ].equals(guess1))
    {
    System.out.println("You have already guessed that letter");
    }
    */





    import java.lang.*;
    import java.util.*;
    import java.io.*;
     
    public class Program
    {
        /**
         * This is the main entry point for the application
         */
        static Scanner console = new Scanner(System.in);
        public static void main(String args[]) 
        {
            System.out.println("Welcome to Hangman!");
            System.out.println();
            System.out.println("The word is:  _ _ _ _ "); // the word is cave
            String correctguess = "_ _ _ _";
            String cave = "cave";
            boolean z = false;
            String replace1 = "1234";
            int i = 0;
                do
                {
                    int x = 0;
                    int y = 0;
                System.out.println("Please enter your one letter guess");
                String guess1 = console.next();
                    guess1 = guess1.toLowerCase();
                String guesshold[] = new String[11];
                    guesshold[x] = guess1;
     
     
     
                if(guess1.equals("c"))
                {
                    System.out.println("Nice " +guess1+ " is a correct letter!");
                    replace1 = replace1.replace('1' , 'c');
                }
                   else if(guess1.equals("a"))
                   {
                       System.out.println("Nice " +guess1+ " is a correct letter!");
                       replace1 = replace1.replace('2' , 'a');
                   }
                    else if(guess1.equals("v"))
                    {
                        System.out.println("Nice " +guess1+ " is a correct letter!");
                        replace1 = replace1.replace('3' , 'v');
                    }
                    else if(guess1.equals("e"))
                    {
                        System.out.println("Nice " +guess1+ " is a correct letter!");
                        replace1 = replace1.replace('4' , 'e');
                    }
     
                    else
                    {
     
                        	String wrong1 = "XXXXXXX";
                           wrong1 = wrong1.substring(0,i);                 
                           System.out.println("Wrong X" +wrong1);
                           int wronglength = wrong1.length();
                           i++;
                           if(wronglength == 7)
                           {
                                  z = true;
                              }
                    }               
                    if(replace1.equals(cave))
                    {
                        z = true;
                        System.out.println("You WON the guessed the correct letters in the word CAVE");
                    }
                    for(int counter = 0; counter <= 10; counter++)
                    {
                        if(guesshold[counter +1 ].equals(guess1))
                        {
                            System.out.println("You have already guessed that letter");
                        }
                    }
     
                }while(z == false);    
        }//ends main method
        }//ends class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Hangman program! Need help reading an ar

    the program ends after the first letter is entered..
    Does it end with an error? If so, please copy the full text and paste it here.
    Otherwise what happens when? Can you copy the full contents of the console and paste it here that shows what the program prints out?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Hangman program! Need help reading an ar

    no the program runs but logically it does not work. when i run it and enter a letter the program just ends itself..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Hangman program! Need help reading an ar

    the program just ends itself..
    Without any errors?

    Try debugging the code by adding some println statements to show where the execution goes and to show the values of variables as they are used and changed. If you add enough println statements you'll know where the execution stopped by seeing which println did not print which will tell you that the program stopped executing before it got to that println.
    Copy the console's contents and paste it here when done testing if you have questions about what the code is doing as it executes.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    mmagyar14 (January 15th, 2013)

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Java Hangman program! Need help reading an ar

    Quote Originally Posted by mmagyar14 View Post
    no the program runs but logically it does not work. when i run it and enter a letter the program just ends itself..
    Not for me. I get a "java.lang.NullPointerException". This occurs at the code you mention in your first post -> either after a successful or unsuccessful guess is made.

    My advice would be to improve your coding style by picking better variable names. Calling the code 'Program' is also not wise.

    String cave = "cave";
    (maybe call this 'secretWord' or something equally more appropriate)

    Your code also needs to be more generic to handle different words.

Similar Threads

  1. Hangman program
    By psgtyler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 12:58 AM
  2. Help with java hangman game(beginner)
    By alexander@semfe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2012, 01:56 PM
  3. Java Hangman!
    By JavaManNoob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 11:17 PM
  4. Need help with my hangman program!
    By mbae in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 29th, 2012, 07:24 PM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM