# Very Simple Basic Java Hangman Program

I wrote this hangman program. It takes three words in an array, picks a random word and then the user guesses the letters. It's lengthy, but it's extremely simple and understandable. Suggestions are welcome, but keep in mind, my knowledge in java is limited.

import java.util.*;
public class Hangman
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int  noOfGuesses =0,lettersCorrect=0;
String choiceToPlay, userGuess;
String[] list={"hello","java","loop"};
System.out.println("\t\t\t\tWelcome to Hangman!");//main game start menu
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
do
{  System.out.println("\t\t\t  Do you want to play? Press Y/N\n");
choiceToPlay = sc.next();
{
System.out.println("\t\t\t\tYou have entered an invalid option. Try again!\n");
}//if any letter other than y or n is entered
}//do
{
System.out.println("You have chosen to leave the game.");
System.out.println("Goodbye!");
System.exit(0);
}//if
else
{
int randomWordNumber = (int)((Math.random()*list.length));// Pick random index of guessWord array
char[] enteredLetters = new char[list[randomWordNumber].length()];// Create an array to store already entered letters
switch(randomWordNumber)
{
case 1:
{
char [] correctLetter = {'-', '-', '-', '-', '-'};
while(lettersCorrect <5)
{
char guessWord[]={'h','e','l','l','o'};
System.out.println("\nThe guess word has 5 letters.");
System.out.println("Enter a letter to guess: ");
userGuess = sc.next();
letter = userGuess.charAt(0);//Incrementing letters each time.
noOfGuesses++;
if(letter == guessWord[0])
{
System.out.println("There is 1 "+guessWord[0]+" in the word");
System.out.println("You have guessed the first letter correctly.");
correctLetter[0] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect++;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if first letter
else if(letter==guessWord[1])
{
System.out.println("There is 1 "+guessWord[1]+" in the word");
System.out.println("You have guessed the second letter correctly.");
correctLetter[1] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect++;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if second letter
else if(letter ==guessWord[2]||letter==guessWord[3])
{
System.out.println("There are 2 "+guessWord[2]+" in the word");
System.out.println("You have guessed the third and fourth letters correctly.");
correctLetter[2] = letter;
correctLetter[3]=letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect+=2;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if third and fourth letters
else if(letter ==guessWord[4])
{
lettersCorrect++;
System.out.println("There is 1 O in the word");
System.out.println("You have guessed the fifth letter correctly.");
correctLetter[4] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
System.out.println("Letters correct so far: "+lettersCorrect);
}//if fifth letter
else
{
System.out.println("The letter you guessed is not in the word.\n");
System.out.println("Guesses taken so far: "+noOfGuesses);
System.out.println("Letters correct so far: "+lettersCorrect);
}//else incorrect letter
}
System.out.println("\nYou found the word!");
System.out.println("It was hello.");
System.out.println("Total guesses: "+noOfGuesses);
break;
}
case 2:
{
char [] correctLetter = {'-', '-', '-', '-'};
while(lettersCorrect <4)
{
char guessWord[]={'j','a','v','a'};
System.out.println("\nThe guess word has 4 letters.");
System.out.println("Enter a letter to guess: ");
userGuess = sc.next();
letter = userGuess.charAt(0);//Incrementing letters each time.
noOfGuesses++;
if(letter == guessWord[0])
{
System.out.println("There is 1 "+guessWord[0]+" in the word");
System.out.println("You have guessed the first letter correctly.");
correctLetter[0] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect++;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if first letter
else if(letter==guessWord[1])
{
System.out.println("There are 2 "+guessWord[1]+" in the word");
System.out.println("You have guessed the second and fourth letters correctly.");
correctLetter[1] = letter;
correctLetter[3] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect+=2;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if second letter
else if(letter ==guessWord[2])
{
System.out.println("There is 1 "+guessWord[2]+" in the word");
System.out.println("You have guessed the third letter correctly.");
correctLetter[2] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect++;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if third and fourth letters
else
{
System.out.println("The letter you guessed is not in the word.\n");
System.out.println("Guesses taken so far: "+noOfGuesses);
System.out.println("Letters correct so far: "+lettersCorrect);
}//else incorrect letter
}//while loop
System.out.println("\nYou found the word!");
System.out.println("It was java.");
System.out.println("Total guesses: "+noOfGuesses);
break;
}
default:
{
char [] correctLetter = {'-', '-', '-', '-'};
while(lettersCorrect<4)
{
char guessWord[]={'l','o','o','p'};
System.out.println("\nThe guess word has 4 letters.");
System.out.println("Enter a letter to guess: ");
userGuess = sc.next();
letter = userGuess.charAt(0);//Incrementing letters each time.
noOfGuesses++;
if(letter == guessWord[0])
{
System.out.println("There is 1 "+guessWord[0]+" in the word");
System.out.println("You have guessed the first letter correctly.");
correctLetter[0] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect++;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if first letter
else if(letter==guessWord[1])
{
System.out.println("There are 2 "+guessWord[1]+" in the word");
System.out.println("You have guessed the second and third letters correctly.");
correctLetter[1] = letter;
correctLetter[2] = letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect+=2;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if second letter
else if(letter ==guessWord[3])
{
System.out.println("There is 1 "+guessWord[3]+" in the word");
System.out.println("You have guessed the fourth letter correctly.");
correctLetter[3]=letter;
System.out.println(correctLetter);
System.out.println("You have had " + noOfGuesses + " guesses, so far");
lettersCorrect++;
System.out.println("Letters correct so far: "+lettersCorrect);
}//if third and fourth letters
else
{
System.out.println("The letter you guessed is not in the word.\n");
System.out.println("Guesses taken so far: "+noOfGuesses);
System.out.println("Letters correct so far: "+lettersCorrect);
}//else incorrect letter
}//while loop
}//case
System.out.println("\nYou found the word!");
System.out.println("It was loop.");
System.out.println("Total guesses: "+noOfGuesses);
}//switch
}
}
}


Your code is pretty straightforward and easy to follow. There are several things that you can do, some of which I've suggested below, to make it easier to update and to form a basis for future programs.

# main

You've written all of your code in the main function. This is a fairly typical approach for people that are learning the language. You keep adding the logic as required until you get to the end. It can quickly become difficult to extend or update. Ideally you want to break main up into logical elements (other classes & functions) that can be connected to form your program. Each element should have a distinct responsibility and should be named accordingly. This will make the individual elements easier to understand and make it easier to identify candidates for reuse in the future.

# Remove duplication

You've written a lot of code. A lot of it is however very similar to other bits. Where you notice duplication it is often a good sign that you're missing a building block (such as a function). Looking at your code, you could break the code up into a structure like this:

playAgain = askPlayerIfTheyWantToPlayAgain()
if(playAgain) {
wordToGuess = getRandomWord();
guessLettersUntilWin(wordToGuess);
}


You'll notice that I've contracted the case statements for each word into a single method guessLettersUntilWin, which I pass the word into. That's because there is a lot of duplication between the different case statements that you should be able to remove. You can use wordToGuess.length() to get the length of the word to guess for example, rather than hard coding the value (4/5). Rather than hard coding the letters for the word to guess you can use wordToGuess.toCharArray(). etc.

# Nesting

Too many nesting levels are bad. They make the code wander across the page to the right until it falls off the edge of your screen. You can avoid excessive nesting by breaking your code into appropriate methods. You can also avoid using it when it isn't necessary. For example, at the start of your program you do:

if(menuChoice == 'N'||menuChoice =='n')
{
System.out.println("You have chosen to leave the game.");
System.out.println("Goodbye!");
System.exit(0);
}//if
else
{
// Rest of code


And then the rest of the code is contained within the else clause. The if branch never escapes, so you don't need the else. You can simply do:

if(menuChoice == 'N'||menuChoice =='n')
{
System.out.println("You have chosen to leave the game.");
System.out.println("Goodbye!");
System.exit(0);
}
// Rest of code


You don't typically see many comments on closing braces indicating why the brace was opened, it just adds clutter to the code. They can be useful where you have large blocks of nested code, however as I've said above this is often an indication that you need to break your code up more.

# loops can be your friend

Your case statements again, have a lot of duplication in them. This is because you've created an if/else chain for each letter in the word. This structure could be replaced with a for loop (pseudocode):

for(index=0 to guessWord.length()) {
if(letter == guessWord[index]) {
print("You found...")
lettersCorrect++;
}
}
// check if lettersCorrect == guessWord.length()