Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

hangman program, err: cannot find symbol, buffered reader? Java newbie

$
0
0
Here is the part part of my program with most of the errors:

    //play method
        private void play() throws IOException
    	{
            do {
			guess(alphabet);

			usedLetters(used, alphabet);

			do{
				System.out.println("You have already guessed this " + alphabet + " and the others listed above.");
    			guess(alphabet);
    		} while (letterUsed == true);//will keep asking until letter was not used

    		if (letterUsed == false)
    		{
    			life--;
    			underWord(currentWord , checkLetter, used);
    		} //only if the letter was not used, it will be checked for in the current word trying to be solved

    		if (life == 0)
    		{
    			checkWord(currentWord , checkLetter);
    		}
            } while (checkLetter != currentWord);

    	}//End of play method

//------------------------------------------------------------------------------------------------------------------------------

/*
This method in particular has all the errors at the moment. I need to ask for the user's guess of 1 alphabet, then check if it is in the word that they are currently trying to guess. I also store all the letters they have already guessed to make sure that they wont lose lives for guessing the same letter twice. So her I have three arrays, one to store the used letters, one to store the parts of the word that have been already guessed(and parts where an _ is necessary) and one that stores the word they are trying to guess. Is there a better way I could write this method below to do its required task?

	//underWord method to return the word with all characters not in letters replaced by _'s
	private void underWord(char currentWord[] , char checkLetter[], char used[]) throws IOException //add parameters what it recieves
	{

        for (int i = 0; i<currentWord.length; i++) {
           for (int index = 0; index<checkLetter.length; index++){
           		if (used.contains(currentWord.charAt(i)))
            	{
                	checkLetter[index] =  currentWord.charAt(i);
            	}

            	else
            	{
            		checkLetter[index] = '_';
            	}
       	   }
		}
        for (int i = 0; i < checkLetter.length; i++)
        {
        	System.out.println(checkLetter[i]);
        }
    }//End of maskWord method


/*
I compile and the following shows:
C:\Users\HOME\Documents\JCreator LE\MyProjects\twoHangman\src\twoHangman.java:289: error: cannot find symbol
if (used.contains(currentWord.charAt(i)))
^
symbol: method charAt(int)
location: variable currentWord of type char[]
C:\Users\HOME\Documents\JCreator LE\MyProjects\twoHangman\src\twoHangman.java:291: error: cannot find symbol
checkLetter[index] = currentWord.charAt(i);
^
symbol: method charAt(int)
location: variable currentWord of type char[]
C:\Users\HOME\Documents\JCreator LE\MyProjects\twoHangman\src\twoHangman.java:312: error: cannot find symbol
alphabet = kb.readline().charAt(0);
^
symbol: method readline()
location: variable kb of type BufferedReader

Its my first time using Buffered Reader, and I am not sure what is wrong
*/
//------------------------------------------------------------------------------------------------------------------------------

    //guess method to ask for user's guess
    private void guess(char alphabet) throws IOException//add parameters what it recieves
    {
    	System.out.println("Guess a letter in this 5-letter word: ");
    	alphabet = kb.readline().charAt(0);
    	used[dataSize] = alphabet;
    	//adds guess to used letters array

    }//End of guess method

//------------------------------------------------------------------------------------------------------------------------------

    //usedLetters method to check if user's guess has already been guessed
    private boolean usedLetters(char used[], char alphabet) throws IOException
    {
        for(int x=0; x<used.length; x++)
        {
            if(alphabet == used[x])
            {
                letterUsed = true;
                return letterUsed;
            }
            else
            {
                letterUsed = false;
                return letterUsed;
            }
         }

         for (int index = 0; index < used.length; index++) {
            System.out.println(used[index]);
        }


    }//End of usedLetters method

//------------------------------------------------------------------------------------------------------------------------------

    //checkWord method to see if the user has got the correct word
    private void checkWord(char currentWord[], char checkLetter[]) throws IOException
    {
        for(int x=0; x<currentWord.length; x++)
        {
           	if(checkLetter == currentWord)
            {
            	System.out.println("HUZZAH! You have guessed the word!");
            	System.out.println("You have completed this game of hangman.");
            	menu();
            }

            {
                System.out.println("You have run out of guesses!");
                System.out.println("The word was:");
                	for (int index = 0; index < currentWord.length; index++)
                	{
                		System.out.print(currentWord[index]);
                	}
				menu();
        	}
    	}
    }//End of checkWord method
}

//------------------------------------------------------------------------------------------------------------------------------


Any help would be appreciated!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>