Main method problem


#1

I having some trouble with this and it's night , so it says I have 4 errors in my main method. I made sure to copy and paste too but still said the same thing,I don't know what wrong and java error messages aren't the best tellers :sob:
Thanks in advanced just in case I fall asleep and don't reply.

P.S: I made sure to check forums before writing this just in case there was a solution there.

Error:
Calculator.java:33: error: class, interface, or enum expected
public static void main(String[] args) {
^
Calculator.java:35: error: class, interface, or enum expected
System.out.println(myCalculator.add(5, 7));
^
Calculator.java:36: error: class, interface, or enum expected
System.out.println(myCalculator.subtract(45, 11));
^
Calculator.java:37: error: class, interface, or enum expected
}
^
4 errors

//a working calculator
public class Calculator {
  public Calculator(){
    
  };
 public int add(int a, int b){
    return a + b;
  };
 
 public int subtract(int a, int b){
    return a - b;
  };
 public int multiply(int a, int b){
    return a * b;
  };
 public int divide(int a, int b){
    if(b = 0){
      System.out.println("Error! Dividing by zero is not allowed.");
      return 0;
    }else{
      return a / b;
    }
 }
  public int modulo(int a, int b){
    if(b = 0){
      System.out.println("Error! Dividing by zero is not allowed.");
      return 0;
    }else{
      return a % b;
    };
  }
  };
	public static void main(String[] args) {
		Calculator myCalculator = new Calculator("myCalculator");
    System.out.println(myCalculator.add(5, 7));
    System.out.println(myCalculator.subtract(45, 11));
  }


#2

Read the first error, fix it, then re-run. Repeat.

If you're able to tell that they're not related then you can read more than one at once, but otherwise just take care of the first one which is the first problem the compiler encountered.

Like the error message says, only those things listed are allowed at the top level of the file.


#3

Thank you it took awhile to do it but I looked for 20 minutes and found all errors and fixed them


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.