Search code examples
javaclasscurly-bracesbraces

Code fails when class definition closed


I'm a beginner to Java trying to understand why my program works when it seems like it shouldn't. When I try and compile the code below, I get the error "class, interface, or enum expected" and it points to the brace on the final line of the program. If I remove the brace, the code compiles and then runs perfectly. But it seems to me that this brace should be necessary to close the definition of the class. Can anyone tell me what is going on? I am simply running the code from the terminal in a Linux environment. Thanks!

class Fermat 
{
  public static void main(String[] args) 
  {
    checkFermat(3,4,5,3);
  }
  public static void checkFermat(int a, int b, int c, int n) 
  {
    if (Math.pow(a,n)+Math.pow(b,n) == Math.pow(c,n))
    {
      System.out.println("Fermat was wrong!");
    } 
      else 
      {
        System.out.println("Nope.");}
      }
  }
}

Solution

  • There's an extra unnecessary } in your app. I thing you just put extra one at line System.out.println("Nope.");} Remove it and the code will compile, making your IDE happy =)

    Or remove any other closing } at the end of the program.