0
import java.util.Scanner;
import java.util.ArrayList;

public class hangman {

    public static void ttt(String inputWord) {
        int wordLength = inputWord.length();
        String blanks = "";
        for (int i = 0; i < wordLength; i++) {
            blanks = blanks.concat("_ ");
        }
        // System.out.print(wordLength);
        System.out.println(blanks);
        int points = 0;
        int counter = 0;
        ArrayList<String> usedChars = new ArrayList<String>();
        while (points < wordLength) {
            Scanner reader = new Scanner(System.in);
            System.out.println("Guess: ");
            String guess = reader.next();
            // int checker = 0;
            // for(int k = 0; k < usedChars.size(); k++) {
            // if(usedChars.get(k) != guess) {
            // checker = checker + 1;
            // }
            // else {}
            // }
            // if(checker == usedChars.size()) {
            for (int i = 0; i < wordLength; i++) {
                if (guess == inputWord.substring(i, i + 1)) {
                    points = points + 1;
                    usedChars.add(guess);
                    System.out.println("hit"); // this is me trying to see if
                                               // this part is working
                } else {
                }

            }
            System.out.println("Used letters: " + usedChars);
            // }
            // else {
            // System.out.print("Sorry, that letter has already been used");
            // }
            counter = counter + 1;
            if (counter == 5) {
                points = wordLength;
            }

        }

        System.out.println("Game over");
    }

    public static void main(String[] args) {
        ttt("to");

    }

}

Don't worry about the commented out code, that's just me going over the top trying to prevent duplicate guesses, but it's more important that I get the rest of the code to work first.

Anyway, the only part of my code that seems to be working is the counter part. You can try it yourself, but it seems like all it does it take 5 guesses (5 lives, kind of random) and print game over.

edit: in hindsight i need to revisit that counter part, because it should only increase for incorrect guesses

The first thing I noticed was that my array isn't working correctly. It's not .add()'ing like I ask it to add my guesses. (Source: https://beginnersbook.com/2013/12/java-arraylist-add-method-example/)

Then, the more serious problem of the code not even being able to record correct guesses :/

I'm starting to code in my school's java class and decided to try this on my own for fun, any help would be greatly appreciated! :P

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
notacorn
  • 3,526
  • 4
  • 30
  • 60
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Sep 22 '17 at 03:37
  • @HovercraftFullOfEels I just fixed up my code entirely and I'm really happy, where can i go to share my game? :) – notacorn Sep 22 '17 at 04:06

1 Answers1

0

change the following boolean expression

guess == inputWord.substring(i, i + 1)

to

guess.equals(inputWord.substring(i, i + 1))

because guess is a String object. using '==' operator will only compare the reference, not the value.

also you might want to use

String guess = reader.nextLine();

instead of

String guess = reader.next();
Tan Wei Lian
  • 92
  • 1
  • 9
  • wow thanks! (needed more characters) pretty sure the equality operator is more flexible in other languages but java just has to be this way I guess – notacorn Sep 22 '17 at 03:43