0

I was trying to create a java program that adds 2 numbers but keep getting this error

error: '.class' expected  
  return int ad();              

1 error  
error: compilation failed

Here is my code

public class Sum {
    int a;
    int b;
    int add;

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

    public static void main(String[] args) {
        return int ad();
    }

}
Turing85
  • 18,217
  • 7
  • 33
  • 58
Angie
  • 1
  • 2

3 Answers3

1
public class Sum {
    /*
    int a;
    int b;
    int add;
    */

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

    public static void main(String[] args) {
        int sum = ad(1, 3);
        System.out.println(sum);
    }
}
  1. main()'s return type is void - it cannot return anything. Mostly it is used to call functions.
  2. Pass parameters to ad() - or you will get a compile time exception - it is expecting 2 integers.
  3. Redundant casting here: int add = (int) a + b; - for a simple method like this, you can directly return a + b;
  4. Unused variables - all your member variables are unused.

Solution 2 (using member variables):

public class Sum {
    private int a;
    private int b;

    private int ad() {
        return a + b;
    }

    public static void main(String[] args) {
        Sum s = new Sum();
        s.a = 1;
        s.b = 2;
        int sum = s.ad();
        System.out.println(sum);
    }
}
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

There are some points in your code:

  1. main() can't return anything because it's a void method.
  2. You don't have to use member variables in this situation. Member variables are most used in classes. Like for instance when you want to create a Person class.
  3. You don't have to type int add = (int) a + b because you don't have to convert anything.The datatype is already an int.

Here's an example of what you can do:

public class Main {

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

public static void main(String[] args) {
   Main main = new Main();
   System.out.println(main.add(1, 1));
}

}

Output:

2

Hope this information was useful!

huntgang99
  • 61
  • 3
0

The method "public static void main(String[] args)" is static and the method "public public int ad(int a, int b)" is non-static.

If you want to reach the method "public int ad(int a, int b)" then make an instance of class Sum and call the method "ad(int a, int b)", or make the method "ad(int a, int b)" static. As already mentioned in comments above, "public static void main(String[] args)" has no return type - it is void, so no "return int ad()" in main method is needed.

Alrernative 1 - Make an instance of class Sum and call method ad(int a, int b):

public class Sum {
    int a;
    int b;
    int add;

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

    public static void main(String[] args) {
        Sum sum = new Sum(); // Make an instance of class Sum
        int result = sum.ad(1, 2); // and call method ad
        System.out.println("Result: " + result); // Output: 'Result: 3'
    }
}

Alternative 2 - Make method ad(int a, int b) static:

public class Sum {

    public static int ad(int a, int b) { // Make method ad static
        int add = (int) a + b;
        return add;
    }

    public static void main(String[] args) {
        int result = Sum.ad(1, 3); // Calling static method ad
        System.out.println("Result: " + result); // Output: 'Result: 3'
    }
}

Read more about diffrence between static and non-static methods here: https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method#:~:text=A%20static%20method%20belongs%20to%20the%20class%20and%20a%20non,class%20that%20it%20belongs%20to.&text=In%20the%20other%20case%2C%20a,class%20has%20already%20been%20instantiated.

DigitShifter
  • 801
  • 5
  • 12