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

Thread: JAVA HANGMAN GAME

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default JAVA HANGMAN GAME

    package worksheet7Quest16;
     
    import java.util.Scanner;
     
    public class Test2 {
    	 Scanner in = new Scanner( System.in);
         int attempts = 0;
    	 Scanner userInput = new Scanner(System.in);
         String secretWord = userInput.next();
         int len = secretWord.length(); //Store the length which will be used to see if puzzle was solved.
         char[] temp = new char[len]; //Store a temp array which will be displayed to the user
         for(int i = 0; i < temp.length; i++) //initialize the array
         {
             temp[i] = '*';
         }
         System.out.print("\n");
         System.out.print("Word to date: ");
         while (attempts <= 10 && attempts > 0)
         {
             System.out.println("\nAttempts left: " + attempts);
             System.out.printf("Enter letter: ");
             String test = userInput.next();
     
             if(test.length() != 1) 
             {
                 System.out.println("Please enter 1 character");
                 continue;
             }
     
             char testChar = test.charAt(0);
     
             //Find matches
             int foundPos = -2;
             int foundCount = 0; //How many matches did we find
             while((foundPos = secretWord.indexOf(testChar, foundPos + 1)) != -1)
             {
                 temp[foundPos] = testChar; //Update the temp array from * to the correct character
                 foundCount++;
                 len--; //Decrease overall counter
             }
     
             if(foundCount == 0)
             {
                 System.out.println("Sorry, didn't find any matches for " + test);
             }
             else
             {
                 System.out.println("Found " + foundCount + " matches for " + test);
             }
     
             //Print 
             for(int i = 0; i < temp.length; i++)
             {
                 System.out.print(temp[i]);
             }
             System.out.println();
     
             if(len == 0)
             {
                 break; //Solved!
             }
     
             attempts--;
         }
     
         if(len == 0)
         {
             System.out.println("\n---------------------------");
             System.out.println("Solved!");
         }
         else
         {
             System.out.println("\n---------------------------");
             System.out.println("Sorry you didn't find the mystery word!");
             System.out.println("It was \"" + secretWord + "\"");
         }
    }
    }


    I am getting an error when initalizing : char[] temp = new char[len];
    Please help


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: JAVA HANGMAN GAME

    please paste the error message here

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HANGMAN GAME

    syntax error on token ";", { expected after this token

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: JAVA HANGMAN GAME

    do you have a main method? you cannot do the algorithm outside the method.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HANGMAN GAME

    A single program is required. That's all i have.

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: JAVA HANGMAN GAME

    Yeah, your Test2 class is a single program.
    but you cannot make any program algorithm outside method.
    you cannot declare a loop, print something outside the method, using methods of different class, etc, etc otherwise you'll get compilation error.
    your create a method or main method then put that code inside the method. that is the only way to get rid of that error

    A single program is required. That's all i have.
    creating a method and putting your code inside that method will not make your Test2 class a multiple program it will still a single program

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HANGMAN GAME

    Thank you, will re-read the question an retry. My new attempt will be posted tomorrow.

  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: JAVA HANGMAN GAME

    Quote Originally Posted by Java girl View Post
    A single program is required. That's all i have.
    This is an ambiguous comment. Please be more clear when stating requirements.

    Most would allow (even expect) that a 'single program' would include multiple methods and classes.

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

    dicdic (March 27th, 2014)

  10. #9
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: JAVA HANGMAN GAME

    Quote Originally Posted by GregBrannon View Post
    This is an ambiguous comment. Please be more clear when stating requirements.

    Most would allow (even expect) that a 'single program' would include multiple methods and classes.
    ow, I over looked at that. thanks. At first, I assumed that she was trying to say it to do it in a single class.

  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: JAVA HANGMAN GAME

    Please don't start multiple threads on the same topic.

    Thread closed. Link to succeeding thread.

Similar Threads

  1. Hangman Game
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 12:50 PM
  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. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 03:15 PM
  4. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM