Why is my program constantly ending? How do I fix this?

Viewed 44

import java.util.; import java.io.;

public class Hangman {

public static void main(String[] args) throws Exception {
    // read file and title
    System.out.println("H A N G M A N");
    System.out.println("________________________");
    ArrayList<String> words = new ArrayList<String>();
    ArrayList<Integer> indexes = new ArrayList<Integer>();
    ArrayList<String> spaces = new ArrayList<String>();
    File file = new File("C:\\Users\\nithi\\OneDrive\\Documents\\temp\\hangmanwords.txt");
    Scanner reader = new Scanner(file);
    Scanner input = new Scanner(System.in);
    int numLives = 7;

    while(reader.hasNextLine())
    {
        words.add(reader.nextLine()); // read a file using the Scanner class in this way.
    }

    int num = (int)(Math.random() * words.size()) + 1;
    String word = words.get(num);
    for(int i = 0; i < word.length(); i++)
    {
        spaces.add("_ ");
    }

    for(int i = 0; i < spaces.size(); i++)
    {
        System.out.print(spaces.get(i));
    }

    while(numLives > 7)
    {
        if(numLives == 0)
        {
            break;
        }
        System.out.println("Enter guess of letter: ");
        String letterGuess = input.nextLine();
        if(word.contains(letterGuess)) // if the word contains the letter guessed, then we must go through the word to find the indexes of each of the parts that contain the letterGuess. Replace all the indexes of the word with the letterGuess inputted.
        {
            for(int i = 0; i < word.length() - 1; i++)
            {
                if(word.substring(i, i + 1) == letterGuess)
                {
                    indexes.add(word.indexOf(letterGuess));
                }
            }
            for(int i = 0; i < word.length() - 1; i++)
            {
                if(word.charAt(indexes.get(i)) == '_')
                {
                    word.replaceAll("_ ", letterGuess);
                }
            }
        }
    }

}

}

I have a problem with program constantly ending when running. How do I make it so it does not do this, and actually goes through until the end of my code?

I have tried getting rid of comments, I have tried commenting out certain parts of code, but all attempts have not resolved my issue. Is there anything I am missing here?

1 Answers

Your issue is that you start off by setting numLives to 7 and then you run a while loop for while (numLives > 7). This while loop will never run as the condition is never true.

I would suggest replacing the 7 with 0, meaning that the while loop will run for as long as the variable numLives is greater than 0. This also means that you can get rid of the check for if numLives is equal to 0 (see below):

    while(numLives > 7)
    {
        if(numLives == 0)
        {
            break;
        }

to

    while(numLives > 0)
    {

However, this will run forever as numLives is never decreased from 7. To fix this, I would suggest adding an else statement after the if statement that checks if the letter is in the word. Inside this else statement, you would decrease numLives (i.e. numLives--;), as the letter is not in the word.

Related