I am trying to write a program in java that plays hangman with the user. However, the for loop to check if a letter is correct is not working. I am having difficulty ascertaining why. The program I am using is Eclipse. Here is just the for loop and the if statement in which it is encased (l is the length of both the word and the arraylist theGuessed, and g is the guessed letter):
if (theWord.contains(g))
{
for (int k = 0; k > l; k++)
{
if (g == theWord.get(k))
{
theGuessed.remove(k);
theGuessed.add(k, g);
b = "true";
System.out.println(theGuessed);
}
}
And here is the whole code:
import java.util.Scanner;
import java.util.ArrayList;
public class HangMan
{
private static String guesses;
private static String b;
private static String e;
private static String m;
private static String n;
private static String o;
private static String p;
private static String q;
private static String r;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ArrayList<String> theWord = new ArrayList<String>();
ArrayList<String> theGuessed = new ArrayList<String>();
guesses = "";
b = "";
e = "";
m = "";
n = "";
o = "";
p = "";
q = "";
r = "";
System.out.println("Player 1, how many letters are in your word?");
int l = input.nextInt();
System.out.println("What is the word that Player 2 should try to guess? Type one letter at a time.");
String d = input.nextLine();
theWord.add(0, d);
for (int i = 0; i< l; i++)
{
d = input.nextLine();
theWord.add(i, d);
}
for (int j = 0; j< l; j++)
{
theGuessed.add(j, "_");
}
System.out.println(theGuessed);
int y = 0;
while(y == 0)
{
System.out.println("Player 2! What is your guess?");
String g = input.nextLine();
if (theWord.contains(g))
{
for (int k = 0; k != l; k++)
{
if (g == theWord.get(k))
{
theGuessed.remove(k);
theGuessed.add(k, g);
b = "true";
System.out.println(theGuessed);
}
}
if(theGuessed == theWord)
{
System.out.println("That's the word! You win!");
y++;
}
}
else if (m == "wrong" && n == "wrong" && o == "wrong" && p == "wrong" && q == "wrong" && r == "wrong")
{
System.out.println(" |");
System.out.println(" O");
System.out.println("/ | \\");
System.out.println(" / \\");
System.out.println("You lose! The word was " + theWord);
y++;
}
else if (m == "wrong" && n == "wrong" && o == "wrong" && p == "wrong" && q == "wrong")
{
System.out.println(" |");
System.out.println(" O");
System.out.println("/ | \\");
System.out.println(" /");
r = "wrong";
}
else if (m == "wrong" && n == "wrong" && o == "wrong" && p == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
System.out.println("/ | \\");
q = "wrong";
}
else if (m == "wrong" && n == "wrong" && o == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
System.out.println("/ |");
p = "wrong";
}
else if (m == "wrong" && n == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
System.out.println("/");
o = "wrong";
}
else if (m == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
n = "wrong";
}
else if (b != "true")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
m = "wrong";
}
guesses = guesses + g + ", ";
System.out.println("Your guesses so far: " + guesses);
}
}
}
I looked through several of the "for loop not being entered" type questions on this site, but all the ones I saw had i == x as their termination requirement. As you can see, that is not the case here.
Thanks in advance for any assistance you might offer!