I was working on some assignments I had for my java class, and I came across this error:
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to QuizApp! Programmed by Chris!");
System.out.println("Please type the number corrisponding to the correct answer.");
int score = 100;
for(int i = 0;i <5;i++) {
if (i == 0) {
System.out.println("What is the capital of America? ");
System.out.println("1) Washington D.C \t2) New York");
System.out.println("3) Philadelphia \t4) Manhatten");
int answer = myScanner.nextInt();
if (answer == 1) {
System.out.println(ANSI_GREEN+"Correct!");
}else{
System.out.println("Either your answer was wrong, or there was a syntax error.");
score = score - 20;
}
if (i == 1) {
System.out.println("What ocean do emporer penguins swim in? ");
System.out.println("1) The Indian Ocean \t2) The Pacific Ocean");
System.out.println("3) The Atlantic Ocean \t4) The Antarctic Ocean");
answer = myScanner.nextInt();
if (answer == 4); {
System.out.println(ANSI_GREEN+"Correct!");
}else{
System.out.println(ANSI_RED+"Either your answer was wrong, or there was a syntax error.");
}
}
}
}
}
}
It said I had an error with my curly brackets here:
if (i == 1) {
System.out.println("What ocean do emporer penguins swim in? ");
System.out.println("1) The Indian Ocean \t2) The Pacific Ocean");
System.out.println("3) The Atlantic Ocean \t4) The Antarctic Ocean");
answer = myScanner.nextInt();
if (answer == 4); {
System.out.println(ANSI_GREEN+"Correct!");
}else{
System.out.println(ANSI_RED+"Either your answer was wrong, or there was a syntax error.");
}
}
I was trying to put an if-else statement inside of an if statement. I know why its giving me this error, but I'm not sure how to fix it. (The error is most likely java thinking that the }else{ is paired with the first if statement.
The problem is here
You have a semicolon after your
if
statement which terminates the condition. Remove it and you should be fine.