Home > database >  exception in thread "main" java.lang.error: unresolved compilation problem, undefined for
exception in thread "main" java.lang.error: unresolved compilation problem, undefined for

Time:11-27

I am new to programming. While starting Functions, I am facing this issue. I tried many things but unable to find solution. Please help.


My Programme:-

import java.util.*;
 
public static void calculateSum(){
    Scanner sc= new Scanner(System.in){
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.print(a b);
    }
    
}
    


public class Functions {
    public static void main(String args[]) {
        calculateSum();
    }

}

Output:-

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        The method calculateSum() is undefined for the type Functions

        at Functions.main(Functions.java:19)

CodePudding user response:

Move your calculateSum method declaration into your class declaration. In Java, methods have to belong to a class.

CodePudding user response:

import java.util.*;

public class Functions {
    public static void main(String args[]) {
        calculateSum();
    }

public static void calculateSum(){
    Scanner sc= new Scanner(System.in){
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.print(a b);
    }
    
}

}
  • Related