Home > Java > body text

Trying to write a simple addition program, why doesn't the main class recognize the 'adder' method?

WBOY
Release: 2024-02-06 09:15:08
forward
736 people have browsed it
Question content

This is the main class

public static void main(string[] args) {
    scanner keyboard = new scanner (system.in);
    int number1;
    int number2;
    int result = 0;
    system.out.println("enter the first number");
    number1 = keyboard.nextint();
    system.out.println("enter the second number");
    number2 = keyboard.nextint();
    result = adder(number1, number2);
    system.out.println(result);
    keyboard.close();
}
}
Copy after login

This is the method class

package Relearn;

    public class methodology {
        public static int adder(int number1, int number2) {
            int num1 = number1;
            int num2 = number2;
            int sum = num1 + num2;
            
            return sum;
            }
        }
Copy after login

I have "adder" in the main class which is private instead of public and it works fine to put two variables together and when I move it to another class it does nothing does, just gives me the error on line 14 [Exception in thread "main" java.lang.error: Unresolved compilation issues: For type testers, method adder(int, int) is undefined In relearn.tester.main(tester.java:14)]


Correct Answer


I found some errors.

  1. You mentioned that earlier you used the adder function in the same class but later you moved it to a different class. Have you imported this function? If not, you can do it in two ways -
  • a) Import class (not needed if same package), ie. import Relearn.methodology; Then call methodology.adder(number1, number2);
  • b) Import static functions, ie. import static Relearn.methodology.adder; - You don't need to change anything in this case.
  1. Please follow the naming convention. Package names should be lowercase and class names should be named in camel case.

The above is the detailed content of Trying to write a simple addition program, why doesn't the main class recognize the 'adder' method?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!