Https://www.codecademy.com/courses/learn-java/projects/basic-calculator

https://www.codecademy.com/courses/learn-java/projects/basic-calculator

I need help with my code. When I run it, I only get one error.

This is the only error I get

Calculator.java:36: error: reached end of file while parsing

Here is my code below. Help me find my mistake:

// July 9 2020
public class calculator {
  public calculator(){

  }

public int add (int a, int b) {
  add = a + b; 
  return add; 
  }

public int subtract (int a, int b) {
  subtract = a - b; 
  return subtract; 
  } 

public int multiply (int a, int b) {
  multiply = a * b; 
  return multiply; 
  }

public int divide (int a, int b) {
  divide = a / b; 
  return divide; 
  }

public int modulo (int a, int b) {
  modulo = a % b; 
  return modulo; 
  }

public static void main(String[] args) {
  calculator myCalculator = new calculator(); 
  System.out.println(myCalculator.add(4,7));
  System.out.println(myCalulator.subtract(45,11)); 
}
1 Like

Hi!
Thanks for sharing and welcome to the forums :grinning:

You have two main issues. Missing bracket at the very end, and every time you declare a variable in your methods you should have to be specific about type.

For example:

public int add (int a, int b) {
  add = a + b;  // this line is going to throw an error, what's missing? 
  return add; 
  }

For the bracket issue, I recommend using spacing/indentation that helps you see clearly what methods are in the class. It’s good practice and makes for more readable code.

You’re code should compile after you cover these two items.

** edit: watch out for spelling mistakes

System.out.println(myCalculator.add(4,7));
System.out.println(myCalulator.subtract(45,11));
2 Likes

My code still doesn’t work

// July 10 2020
public class calculator {
  public calculator(){

  }




public int add (int a, int b) {
  int add = a + b; 
  return add; 
}

public int subtract (int a, int b) {
  int subtract = a - b; 
  return subtract; 
  } 

public int multiply (int a, int b) {
  int multiply = a * b; 
  return multiply; 
  }

public int divide (int a, int b) {
  int divide = a / b; 
  return divide; 
  }

public int modulo (int a, int b) {
  int modulo = a % b; 
  return modulo; 
  }

public static void main(String[] args) {
  calculator myCalculator = new calculator(); 
  System.out.println(myCalculator.add(4,7));
  System.out.println(myCalculator.subtract(45,11)); 
}
}


How are you running it? It compiles ok on my system:

It also runs fine on my terminal.

Maybe my computer is broken

Capitalize the C in calculator in public class calculator should be public class Calculator

The error message hints at that and will always appear when the title of your public class and file name don’t match (case-sensitive).

2 Likes

Thanks. It works now :slight_smile:

1 Like