-4

I am new to JAVA. I want to create a class and write a function in it. I then want to use that function in the main class.

import java.util.Scanner;

public class multi_fun {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int a, b, c;

        System.out.println("Enter 1st number: ");
        a = scan.nextInt();
        System.out.println("Enter 2nd number: ");
        b = scan.nextInt();
        Addition obj = new Addition();
        c = obj.add(a,b);
        System.out.println("The sum is "+c);
        scan.close();
    }
}
class Addition{
    public int add (int a, int b)
    {
        return(a+b);
    }
}
iamdanchiv
  • 3,828
  • 4
  • 32
  • 40
Vikram Sharma
  • 199
  • 1
  • 2
  • 12

5 Answers5

1

Make sure both the java files are in the same folder.

MultiFun.java

import java.util.Scanner;

public class MultiFun {

    public static void main(String[] args) {
        Addition obj = new Addition();

        Scanner scan = new Scanner(System.in);
        int a, b, c;

        System.out.println("Enter 1st number: ");
        a = scan.nextInt();
        System.out.println("Enter 2nd number: ");
        b = scan.nextInt();

        c = obj.add(a, b);
        System.out.println("The sum is " + c);
        scan.close();

    }

}

Addition.java

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

run the following commands

javac MultiFun.java
java MultiFun
Anto Antony
  • 792
  • 3
  • 11
1

The problem I think according to error message you mentioned in the comments:

Exception in thread "main" java.lang.NoSuchMethodError: Addition.add(II)I at multi_fun.main(multi_fun.java:15)

It seems that you are putting class Addition declaration in the same source file of multi_fun.java program.

You should create a java class file called Addition.java and put your class code in it:

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

After that it should work without any errors.

Update:

You can check this Answer which explain Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'it would be a useful solution to your problem.

Oghli
  • 1,585
  • 1
  • 9
  • 29
  • Do not need to create to Addition.java file in the same class can have multiple class. But make sure all classes are not `public`. And only run main class. – Blasanka May 25 '17 at 13:02
  • 1
    Yes he can do it in that way. but it's better to separate classes file from the main java program file. he should know the good practices to write java program in clean and organized way. also I update my answer and put link to the reasons of this error and the suggested solutions to it. – Oghli May 25 '17 at 14:47
0

If you are run your program using Terminal or command prompt(cmd) make sure to run the class that have main method(created object from Addition class). And also do not create main methods in both classes and do not add public to Addition class.

One last thing: compile and run only the main class(multi_fun). Which is,

javac multi_fun.java
java multi_fun
Blasanka
  • 14,208
  • 6
  • 76
  • 90
0
import java.util.Scanner;

public class multi_fun {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int a, b, c;

        System.out.println("Enter 1st number: ");
        a = scan.nextInt();
        System.out.println("Enter 2nd number: ");
        b = scan.nextInt();
        add(a,b);
        System.out.println("The sum is "+c);
        scan.close();

    }

}


public static int add (int a, int b)
{
    return(a+b);
}
Vandit Upadhyay
  • 240
  • 2
  • 8
0

The reason for this error is that there was already an other file in the same folder named Addition. So when I wrote a Class with the same name and tried to create an object, it was giving error message, as the parameters where different.

Thank you everyone for your help.

Vikram Sharma
  • 199
  • 1
  • 2
  • 12