Im trying to create a Wordle game in Java and i keep getting the error "illegal start of expression" can someone help me with this error and program? Confused on why the error keeps popping up and that is how it was given already.
import java.util.Scanner;
import java.util.Random;
public class Wordle_P3{
public static void main(String[] args) {
// The code for the word of the day is already in place.
final String WORD = getWordOfTheDay();
// Create Scanner Object
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
// For loop that loops 6 times
for(int attempts = 0; attempts <= 6; attempts++){
// Have the user make a guess
System.out.println("Guess a five letter word.");
String guess = keyboard.nextLine();
// Make sure that the guess is five letters
int wordLength = guess.length();
while(wordLength != 5){
System.out.println("Not a five letter word.");
guess = keyboard.nextLine();
wordLength = guess.length();
}
// Checking for a win
String upperGuess = guess.toUpperCase();
if(guess.equals(WORD)){
System.out.println("Correct! You Won!");
return;
}
for(int i = 0; i < 5; i++){
char guessChar = upperGuess.charAt(i);
char feedback = '-';
for(int j = 0; j < 5; j++){
char wordChar = WORD.charAt(j);
if(wordChar == guessChar){
if(i == j)
feedback = guessChar;
else
feedback = guess.toLowerCase().charAt(i);
}
System.out.print(feedback);
System.out.println();
}
System.out.println("Sorry, you lost. Word was "+ WORD);
// Check each letter in guess and see if it is a miss, exct match, or close match
// for each letter in upperGuess
}
}
Random r = new Random();
return WORDS[r.nextInt(WORDS.length)];
private static final String[] WORDS = {
//the rest of the code is a bunch of random words that were given for the program
}
can someone help me with this error and program? Confused on why the error keeps popping up and that is how it was given already.
I believe the code:
belongs within the
getWordOfTheDay()
method which you simply don't provide, for example:It shouldn't be where is resides now.
The WORDS[] array looks to be a Class constant which is declared and filled within the class members area:
In my opinion, the WORDS array should be filled from words list within a text file rather than hard-coded but, a few words for demo purposes, this should suffice.
Your allowing for 6 attempts to guess the randomly provided word but your
for
loop is allowing for 7 attempts:The
attempts <= 6
should beattempts < 6
. Remove the=
from<=
.Here is a runnable example of how this can work. Be sure to read all the comments in code: