Dimitar Markov
Dimitar Markov

Reputation: 51

Can't call a method

I just can't call the method "plus". I was trying to add more curly braces, also normal ones but nothing helped!

package example;
import java.util.Scanner; 
public class HelloWorld {
public static void main(String[] args){
        Scanner a = new Scanner(System.in);
        Scanner b = new Scanner(System.in);
        int varTwo = b.nextInt();
        int varOne = a.nextInt();

        public static void plus (int aa, int bb) {
            return aa+bb;
            }
        plus(varOne, varTwo);
}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    void is an invalid type for the variable plus
    Syntax error on token "(", ; expected
    Syntax error on token ",", ; expected
    Syntax error on token ")", ; expected
    Void methods cannot return a value
    The method plus(int, int) is undefined for the type HelloWorld

    at example.HelloWorld.main(HelloWorld.java:10)

Upvotes: 0

Views: 71

Answers (3)

Darshan Mehta
Darshan Mehta

Reputation: 30819

You're defining a method inside a method, try declaring plus outside of main (and getting rid of another Scanner as you won't need to Scanners), e,g.:

public static void main(String[] args){
        Scanner a = new Scanner(System.in);
        int varOne = a.nextInt();
        int varTwo = a.nextInt();
        int sum = plus(varOne, varTwo);
        System.out.println(sum);
        a.close();
}

public static int plus (int aa, int bb) {
        return aa+bb;
}

Or better, you don't even need a method to perform a simple operation (unless specified), you can do it in main itself (also, you don't need two instances of Scanner as well), e.g.:

public static void main(String[] args){
        Scanner a = new Scanner(System.in);
        int varOne = a.nextInt();
        int varTwo = a.nextInt();
        int sum = varOne + varTwo;
        System.out.println(sum);
        a.close();
}

Upvotes: 0

Michael Dz
Michael Dz

Reputation: 3844

You can't have at the same time void method and return statement inside. If you want to return something from method remove void. Also code after return is unreachable and should be before return statement. Below fixed code that you can continue develop.

import java.util.Scanner; 
public class HelloWorld {

 public static void main(String[] args){
    Scanner a = new Scanner(System.in);
    Scanner b = new Scanner(System.in);
    int varTwo = b.nextInt();
    int varOne = a.nextInt();
    int result = plus(varOne, varTwo);

   }
   public static int plus (int aa, int bb) {
      return aa+bb;
   }

}

Upvotes: 1

Aliaksandr Pitkevich
Aliaksandr Pitkevich

Reputation: 834

Try like this:

package example;
import java.util.Scanner; 
public class HelloWorld {
    public static void main(String[] args){
        Scanner a = new Scanner(System.in);
        Scanner b = new Scanner(System.in);
        int varTwo = b.nextInt();
        int varOne = a.nextInt();


        plus(varOne, varTwo);
    }
    public static plus (int aa, int bb) {
        return aa+bb;
    }

}

Upvotes: 2

Related Questions