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 :)");