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: Hangman

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman

    I'm having a problem with this code, specifically at the "if (guess.indexOf(letter) >= 0))" part, where I get the error: "The method indexOf(String) in the type StringBuilder is not applicable for the arguments (char)"

    I highlight for suggestions and it just creates a loop, so it's just frustrating.

    The other thing, I seem to have a problem doing (and always have for some reason), is that I know a do/while part would help, but I keep putting it in different places, but not working. Any suggestions? I'm not wanting direct answers, but some hints would be helpful.

     import java.util.Scanner;
     
    public class HangmanClass {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String [] words= {"write", "that", "program", "fusion"};
     
    		Scanner input= new Scanner(System.in);
    		char newGame;
     
    		{
    		int index= (int)(Math.random() * words.length);
     
    		String word= words[index];
    		StringBuilder guess= new StringBuilder();
     
    		for (int i=0; i < word.length(); i++){
    			guess.append('*');
    		}
    		int CorrectGuesses=0;
    		int Misses=0;
     
    		while (CorrectGuesses < word.length()){
    			System.out.println("(Guess) Enter a letter in word " +guess+ " > ");
    			String s= input.nextLine();
    			char letter= s.charAt(0);
     
    		if (guess.indexOf(letter) >= 0){
    			System.out.println("\t" +letter+ " is already in the word");}
    		else if (word.indexOf(letter) < 0) {
    			System.out.println("\t" +letter+ " is not in the word");
    			Misses++;}
    		else {
    			int h= word.indexOf(letter);
    			while (h >= 0) {
    				guess.setCharAt(h, letter);
    				CorrectGuesses++;
    				h= word.indexOf(letter, h+1);
     
    				}		
    			}
    		}
    		System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s).");
     
    		System.out.println("Do you want to guess another word? Enter y or n>");	
    	}
     
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,474
    My Mood
    Mellow
    Thanks
    87
    Thanked 484 Times in 475 Posts

    Default Re: Hangman

    Yes, StringBuilder.indexOf() expects a String argument. You can use that method as it requires, or you could use a String method that might be more to your liking after changing the StringBuilder object to a String, of course.

    Explain what you want the do/while loop to do, show what you've tried with any errors, and ask questions about that code.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman

    I'm wanting for the do part to do the hangman game while the user wants to continue playing.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,474
    My Mood
    Mellow
    Thanks
    87
    Thanked 484 Times in 475 Posts

    Default Re: Hangman

    So you do something like:
    do
    {
        while ( puzzleNotSolved )
        {
            // the user plays a game of hangman
        }
     
        // ask the user if another game is desired
     
    }
    while ( userWantsAnotherGame );

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman

    Quote Originally Posted by GregBrannon View Post
    So you do something like:
    do
    {
        while ( puzzleNotSolved )
        {
            // the user plays a game of hangman
        }
     
        // ask the user if another game is desired
     
    }
    while ( userWantsAnotherGame );
    Alrighty, I'll get to it! Thanks.

    *Edit*

    Alright, sweet! I got it. I was close before, but forgot something crucial. I are dum!

Similar Threads

  1. Hangman Game
    By JohanMartin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2014, 05:25 AM
  2. Logic of Hangman
    By mhender24 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 14th, 2013, 06:29 AM
  3. Hangman Game
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 11:50 AM
  4. Hangman Help
    By devindadude in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 25th, 2013, 08:24 PM
  5. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM