0

I am working on a hangman project for school that is due tomorrow and I am using embedded tomcat in java. My program is currently able to accept an input from the servlet and and generate random word from a list that can remain constant while the page reloads. However every time I compare the letter input to the string (it is supposed to add the letter to a char array at the correct index location) it refreshes every time the page reloads so the word in the char array will never be complete- if that makes any sense :(

Here is the code:

private static String currentWord = gameWord();
private static final char[] untilEmpty = new char[currentWord.length()];


    String temp = req.getParameter("Word");
    if (temp == null) {
        currentWord = gameWord();
    } else {
        currentWord = temp;
    }

    for (int i=0; i< currentWord.length();i++){
        untilEmpty[i] = '_';
    }


    System.out.println(currentWord);

    out.write("<form method=\"get\" action=\"game\" >".getBytes());
    out.write("<p> Guess a letter:</p>".getBytes());
    out.write("<input type=\"text\" name=\"Guess\">".getBytes());
    out.write("<input type=\"submit\" value=\"enter here\">".getBytes());
    out.write(String.format("<input type=\"text\" name=\"Word\" value=\"%s\">", currentWord).getBytes());
    out.write("</form>".getBytes());


    String letter = "";
    boolean integer = false;
    try {
        letter = req.getParameter("Guess");
        integer = validateString(letter, 0);
    } catch (Exception e) {

    }

    //String untilEmpty = currentWord;
    String subStr = "";
    String Empty = currentWord;

        if (integer == false) {
            out.write("invalid input".getBytes());

        } else {
            //while (untilEmpty != "") {
            char letterChar = letter.charAt(0);
            if (currentWord.indexOf(letterChar) >= 0) {

                int count = currentWord.length()- currentWord.replaceAll(letter,"").length();
                System.out.println(count);
                Empty = Empty.replaceAll(letter,"");
                System.out.println(Empty);
                for (int i =0; i<currentWord.length(); i++){
                    if (currentWord.charAt(i) == letter.charAt(0)){
                        untilEmpty[i]= letterChar;
                        //untilEmpty.indexOf(letter);
                        //untilEmpty[i] = untilEmpty+letter;
                        //untilEmpty = untilEmpty.substring(i)+letter.charAt(0);

                    }
                }
                System.out.println(untilEmpty);

            } else {
                System.out.println("incorrect guess");
            }
    }

    System.out.println(letter);

    if (Empty == "") {
        System.out.println("guessed whole word :)");
azro
  • 53,056
  • 7
  • 34
  • 70
jelly
  • 1
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – azurefrog Jul 04 '21 at 17:53
  • Not really because the char array collecting all the guesses resets every time a new letter is entered and I don't know how to stop it – jelly Jul 04 '21 at 18:00
  • 1
    Please try to produce a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). That’s a lot of code, without enough context (or surrounding code) for people to run it themselves. – BeUndead Jul 04 '21 at 19:34

1 Answers1

1

I've never used embedded Tomcat but you could try to save state in the Session

g00se
  • 3,207
  • 2
  • 5
  • 9