-1

This is an implementation of the Hangman game. Once the user has uncovered the word, the program should exit, but it continues to prompt the user for input although all letters of the word have been correctly guessed.

How can I make the program exit properly once the word has been guessed?

The word to seach is for example no : Here is an illustration:

Enter your lettre:

n

The letter is OK !

n_

Enter your lettre:

o

The letter is OK !

no

Enter your lettre:

I don't understand why the loop continues ? Because I found the word...

Here is my code:

import java.util.*;

class Main {
  public static void main(String[] args) {

    Scanner input = new Scanner (System.in);
    Random r = new Random();

    String[] arr = {"one", "no"};
    String word = arr[r.nextInt(arr.length)];
    char[] dash = word.toCharArray();
    char[] charArr = word.toCharArray();

    for(int i=0;i<dash.length;i++){
      dash[i] = '-';
      System.out.print(dash[i] + " ");
    }

    System.out.println("");

    for(int i=0;i<3;i++){
      System.out.print("Enter your letter : ");
      char letter = input.next().charAt(0);

      for(int j=0;j<charArr.length;j++){
        if(letter == charArr[j]){
          dash[j] = letter;
          System.out.println("Ok ! ");
          System.out.println(dash);
          i--;
        }
      }

    }

  }
}

I share you the code here => https://repl.it/repls/BonyRevolvingOs

Thank you for your help.

Community
  • 1
  • 1
aurelie
  • 33
  • 5
  • 4
    Why wouldn't it continue? What line(s) of code stop the program once you find the word? – GBlodgett Dec 13 '19 at 18:41
  • 2
    Why would it stop? There is nothing telling it to not continue with the next iteration of `for(int i=0;i<3;i++){}`. You need to add a `break` or something to leave if the word has been figured out before the completion of the iteration. – zero298 Dec 13 '19 at 18:41
  • Question has been edited to include a clear and concise problem statement. Please vote to reopen so it can receive an answer. – MarsAtomic Dec 13 '19 at 18:56
  • [How to break out of a for loop](https://stackoverflow.com/questions/15275195/breaking-out-of-a-for-loop-in-java) – GBlodgett Dec 13 '19 at 19:01
  • @MarsAtomic: I need to solve my problem please; – aurelie Dec 13 '19 at 19:13
  • @ zero298: Where put the `break` in my second loop please? – aurelie Dec 13 '19 at 19:13
  • @aurelie Yeah... I have a solution for you, but people have to vote to re-open the question so I can post it. People are reacting to the original poor problem statement rather than the technical issues involved. Oftentimes, once someone thinks they have the solution, they ignore everything else. – MarsAtomic Dec 13 '19 at 19:14
  • @MarsAtomic: I am stuck so far. I came here to find a solution. :-( – aurelie Dec 13 '19 at 19:29
  • @aurelie Please read the question I linked to you. It explains how to break out of your loop – GBlodgett Dec 13 '19 at 19:47
  • @GBlodgett That's not where the problem is. Vote to re-open. – MarsAtomic Dec 13 '19 at 20:03
  • @MarsAtomic OP wants to know how to break out of a loop. I gave a dupe that shows how to do so – GBlodgett Dec 13 '19 at 20:12
  • @GBlodgett Your solution deals with a symptom, not the underlying problem. You've armed yourself with a hammer. The problem is that not everything you encounter is a nail. – MarsAtomic Dec 13 '19 at 20:15
  • @MarsAtomic Well it appears we disagree. You have cast your vote and I have cast mine. Best of luck to you – GBlodgett Dec 13 '19 at 20:19
  • @GBlodgett: I have tried with a `break` but I am newbie. Do have you an example like for my code please? – aurelie Dec 13 '19 at 20:43
  • _"Because I found the word…"_ — How do you know you found the word? You are not checking if what you have collected in `dash` matches the `word` — that's when you want to move on to the next word or end the game. – Stephen P Dec 13 '19 at 21:23

1 Answers1

1

Look at the block of code defining your inner loop:

for(int j=0;j<charArr.length;j++){
    if(letter == charArr[j]){
        dash[j] = letter;
        System.out.println("Ok ! ");
        System.out.println(dash);
        i--;                           // What is this doing here?
    }
}

At the bottom of that block is the line that's blowing up your program. Get rid of it. Your program never exits the loop because you keep resetting the value of the counter for your outer loop i to 0. Every time the loop increments itself normally to 1, you decrement it, so it's always going from 0 to 1 and back to 0. Because 0 is less than 3, the loop keeps running. If a break statement depended on the value of that counter, it would not have changed the way your program operated at all. If you called break on some other basis, you could have exited, but you would still have had a fundamentally broken program.

This said, you still have a long way to go to get a working implementation of Hangman because as it stands, your user only has as many guesses as the word has letters If you guess wrong, even a single time, you can never complete the word.

You need to find a way to account for incorrect guesses (drawing the parts of the hanged man) and a way to detect and announce victory or failure (right now, your program just exits after two or three guesses whether or not the puzzle is solved). Work on those things, and if you need more help, post separate questions for each problem.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • Thank you MarsAtomic for your help. – aurelie Dec 15 '19 at 19:35
  • @aurelie No problem. I hope you got the rest of your program working. To let people know that the answer helped you, please click the grey checkmark to the left, and we'll both receive some reputation points. – MarsAtomic Dec 15 '19 at 20:18