This is my error message:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at Finder.main(Finder.java:73)
The line in question is this:
else if(Character.isLetter((word).charAt(0)))
Which is part of the larger while loop:
while(true)
{
System.out.print("Please enter the letter you want to guess: ");
word = keyboard.nextLine();
if(word.equalsIgnoreCase("solve"))
{
break;
}
else if(Character.isLetter((word).charAt(0)))
{
word = word.substring(0, 1);
break;
}
else
{
System.out.println("Your input is not valid. Try again.");
}
}
The error message shows up after executing this if statement (which is part of the inner while loop here):
if(difficulty.equalsIgnoreCase("e"))
{
if(spaces == 4)
{
while(hidden.contains(word))
{
if(word.charAt(0) == hidden.charAt(spaces1))
{
dash = dash.substring(0, spaces1) + word + dash.substring(spaces1 + 1, dash.length());
}
if(word.charAt(0) == hidden.charAt(spaces2))
{
dash = dash.substring(0, spaces2) + word + dash.substring(spaces2 + 1, dash.length());
}
if(word.charAt(0) == hidden.charAt(spaces3))
{
dash = dash.substring(0, spaces3) + word + dash.substring(spaces3 + 1, dash.length());
}
if(word.charAt(0) == hidden.charAt(spaces4))
{
dash = dash.substring(0, spaces4) + word + dash.substring(spaces4 + 1, dash.length());
}
if(word.charAt(0) == hidden.charAt(spaces1) || (word.charAt(0) == hidden.charAt(spaces2) || (word.charAt(0) == hidden.charAt(spaces3) || (word.charAt(0) == hidden.charAt(spaces4)))))
{
rightGuess = true;
System.out.println("Your guess is in the word!");
System.out.println("The updated word is " + dash);
}
}
numGuessed = numGuessed - 1;
System.out.println("Your letter was not found in the spaces you provided");
System.out.println("Guesses Remaining: " + numGuessed);
break;
}
}
The break; takes it back to outside while loop asking about the letters, but right after the "Please...?" question, it displays the error message even though I'm unsure why.
(I just added parts of my code because I read that it's inconsiderate to just paste the whole thing. I hope that everything looks clear.)
Aucun commentaire:
Enregistrer un commentaire