Search code examples
javavariablesif-statementintdeclaration

JAVA Variable declaration not allowed here


I get an error "Variable declaration not allowed here" and I don't know why, I'm new in java and can't find answer :/ As it says, I can't make "int" in "if" but is there a way to create it?

import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;import java.util.Scanner;
 public class test{
  public static void main(String[] args) throws FileNotFoundException{
    File plik = new File("test.txt");
    PrintWriter saver = new PrintWriter("test.txt");

     int score = 0;
     System.out.println("Q: What's bigger");
     System.out.println("A: Dog B: Ant");
     Scanner odp = new Scanner(System.in);
     string odpo = odp.nextLine();

     if(odpo.equals("a"))
        int score = 1;
     else
         System.out.println("Wrong answer");

  }
}

Solution

  • string must be changed to String.

    By writing int score you're trying to declare a new variable that already exists, which you declared before already. Just remove the int part and you will get the assignment you want.