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

Thread: Need help with my hangman program!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with my hangman program!

    Hey guys,
    I have to make a Hangman game in Java that asks the user to input their name when you run the program.
    The name is saved to a .txt file that records the WINS and LOSSES for the game.
    There is supposed to be another .txt file that has the words to be used for the game.

    Here is what I have so far:
    PHP Code:
    import java.util.Scanner;
    import java.util.Random;
    import java.io.*;

    public class 
    Hangman {

        private 
    Scanner in = new Scanner(System.in);
                
                private 
    String puzzle;
                private 
    String puzzleWord;
                private 
    int puzzleIndex;
                private 
    Random generator = new Random();
                private 
    boolean win;
                private 
    boolean over;
                private 
    String correct ""
                private 
    char guesses[] = new char[26];
                private 
    int guessed;
                private 
    int misses;
                private 
    int again;
                private 
    String puzzles[] = new String[4];
                
        public static 
    void main(String[] args) {
            
    String letter;
            
    String again "y";
            
            
    Hangman game = new Hangman();
            
            try
            {
                
    BufferedReader in =
                    new 
    BufferedReader(new FileReader("C:\\Users\\Michael\\My Documents\\puzzles.txt"));
                    
                
    int count 0;
                while (
    in.ready())
                {
                    
    game.puzzles[count] = in.readLine();
                    
    count++;
                }
                
    in.close();
            }
            catch(
    IOException e)
            {
                
    System.out.println("Error during reading/writing");
            }
            
            
            
            
    System.out.println ("Welcome to Hangman!");
            
            while(
    again.equals ("y"))
            {
                
    game.init();
                
                while(!
    game.over)
                {
                    
    game.printBoard();
                    
    game.printGuesses();
                    
    System.out.println("Puzzle: " game.puzzle);
                    
    System.out.println("Enter a letter: ");
                    
    letter game.in.next();
                    
                    
    game.guesses[game.guessed] = letter.charAt(0);
                    
    game.guessed++;
                    
    game.sort();
                    
                    if(
    game.puzzleWord.indexOf(letter.charAt(0)) != -1)
                    {
                        
    game.correct game.correct letter.charAt(0);
                        
    game.puzzle game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
                        if(
    game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0)
                        {
                            
    game.win true;
                            
    game.over true;
                        }
                    }
                    else
                        
    game.miss();
                }
                
    game.printBoard();
                
    System.out.println("Solution: " +game.puzzleWord);
                if(
    game.win)
                {
                    
    System.out.println("You've won the game!");
                }
                else
                    
    String again(){
                        
    String again;
                        
    System.out.println("Would you like to play again? (Y/N)");
                        
    again in.next();
                        if(
    again.charAt(0) == 'Y')
                            return 
    "Yes";
                        else
                            return 
    "No";
                }
                
        } 
    the game.printBoard(), game.printGuesses(), game.init(), game.miss() methods are not defined. and I am not too sure what that means. Can anyone help me rewrite that part of the program? I also have not been able to write the code to save a .txt file to record the WINS and LOSSES. And I believe the If else statement at the end that asks you if you want to play again is wrong.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Need help with my hangman program!

    private Scanner in = new Scanner(System.in);

    private String puzzle;
    private String puzzleWord;
    private int puzzleIndex;
    private Random generator = new Random();
    private boolean win;
    private boolean over;
    private String correct = "";
    private char guesses[] = new char[26];
    private int guessed;
    private int misses;
    private int again;
    private String puzzles[] = new String[4];
    This is all the code you use to define your Hangman object; the methods printBoard(), printGuesses(), init(), and miss() are not defined because you haven't defined them. Nowhere in your code do you state something like:

    public void printBoard()

    yet in your main method you make calls to the the printBoard() method as if it exists -- but you haven't made it yet! The program can't read your mind -- if you want it to print the board, you have to explicitly code what that means.

    if(game.win)
                {
                    System.out.println("You've won the game!");
                }
                else
                    String again(){
                        String again;
                        System.out.println("Would you like to play again? (Y/N)");
                        again = in.next();
                        if(again.charAt(0) == 'Y')
                            return "Yes";
                        else
                            return "No";
                }

    Why do you have return statements here? Firstly, the main method almost never returns anything (in fact, it is bad coding to have it return something). Secondly, even if returning values from the main method was okay, you have its return type as void -- meaning it can't return anything!

    As for writing characters to a text file, I suggest a FileWriter.
    Last edited by snowguy13; February 29th, 2012 at 07:27 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Hangman help. Comparing arrays and conditions.
    By javanewbie1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 09:26 AM
  2. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  3. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  4. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM