The Java program gives me the error message: Invalid start of expression?
I use the Notepad app on the Windows operating system to write Java code. Using the "Command Prompt" program, I convert the code into bytecode using JDK-22. My code displays an error message that I don't understand. The exact error message is:
Game1.java:48: Error: Invalid start of expression
private static void movePlayer(Point playerPosition) {
^
1 error
My code is:
import java.awt.Point;
import java.util.Scanner;
public class Game1 {
public static void main(String[] args) {
Point player position = new Point(10, 9);
Point queueposition = new Point(30, 2);
Point key position = new Point(6, 6);
Point door position = new Point(2, 5);
boolean program = true;
boolean keyjn = false;
while (program) {
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 40; x++) {
Point p = new Point(x, y);
if (p.equals(player position)) {
System.out.print("P");
}
else if (p.equals(queuePosition)) {
System.out.print("S");
}
else if (p.equals(keyposition) && keyjn == false) {
System.out.print("G");
}
else if (p.equals(door position)) {
System.out.print("T");
}
else {
System.out.print(".");
}
}
System.out.println("");
}
if (snakeposition.equals(playerposition)) {
program = false;
System.out.println("GAME OVER!");
}
if (player position.equals(key position)) {
keyjn = true;
}
if (playerposition.equals(doorposition) && keyjn == true) {
program = false;
System.out.println("Congratulations, you escaped the snake");
System.out.println("Play again(1), End game(2)");
movePlayer(playerposition);
movequeue(queueposition, playerposition);
}
}
private static void movePlayer(Point playerPosition) { // The error message
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if (input.equals("w") && playerposition.y >= 1) {
playerposition.y–;
}
else if (input.equals("s") && playerposition.y <= 8) {
playerposition.y++;
}
else if (input.equals("a") && player position.x >= 1) {
playerposition.x–;
}
else if (input.equals("d") && playerposition.x <= 38) {
playerposition.x++;
}
}
private static void moveQueue(Point queuePosition, Point playerPosition) {
if (queueposition.x < playerposition.x) {
queueposition.x++;
}
else if (snakeposition.x > playerposition.y) {
queueposition.x–;
}
if (snakeposition.y < playerposition.y) {
snakeposition.y++;
}
else if (snakeposition.y > playerposition.y) {
snakeposition.y–;
}
}
}
I've been searching for the problem for over an hour but haven't found it. I hope you can help me. Thanks in advance for your answers.
That’s because you’re not doing your “main” properly. There’s a ‘}.
Thank you, I didn’t close the if loop. As a result, the program I would have closed the while loop instead of the main method. You saved me the complete passage of my complete code and thus at least one hour of work.
if is not a loop structure. loops can repeat the execution of their assigned code block. A branch like if can’t.
This is a preamble for both structures. He doesn’t make the difference.
Name it at the name: Control structure 🙂