'How can i call the Add() function?
i have been trying to make an easy calculator
import java.util.Scanner;
public class Math {
static Scanner input = new Scanner(System.in);
private static void Add(int a, int b,int c){
System.out.println("First nummber...");
a = input.nextInt();
System.out.println("secound nummber...");
b = input.nextInt();
c = a + b;
System.err.println(c);
}
public static void main(String[] args) {
Add();
}
}
but i got this error
The method Add(int, int, int) in the type Math is not applicable for the arguments ()
I tried then to add the variables to the Add() in the main function
public static void main(String[] args) {
Add(a,b,c);
}
and then I got this error
Multiple markers at this line
- b cannot be resolved to a variable
- c cannot be resolved to a variable
- a cannot be resolved to a variable
Solution 1:[1]
because you haven't declared a,b and c.
try like this.
public static void main(String[] args) {
Add(0,0,0);
}
On side note, you are reading a and b again in your function. You can read them in your main and pass them to function.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = Add(a,b);
System.out.println("Addtion of " + a + " and " + b + " is " + c);
}
and then your Add function will get a and b and just add it.
public static int Add(int a, int b){
return a + b;
}
Solution 2:[2]
All of the other answers have pointed at the problem without providing what I would consider the correct solution. Since you are asking for the input from the user, it is inappropriate to declare the variables as parameters. Instead, you should declare local variables:
public class Math {
static Scanner input = new Scanner(System.in);
private static void Add(){
System.out.println("First nummber...");
int a = input.nextInt();
System.out.println("secound nummber...");
int b = input.nextInt();
int c = a + b;
System.err.println(c);
}
public static void main(String[] args) {
Add();
}
}
Solution 3:[3]
import java.util.Scanner;
public class Math {
static Scanner input = new Scanner(System.in);
private static void Add(){ //do not use arguments here for just calling it
System.out.println("First nummber...");
int a = input.nextInt();
System.out.println("secound nummber...");
int b = input.nextInt();
int c = a + b;
System.err.println(c); //use System.out.println(c);
}
public static void main(String[] args) {
Add();
}
}
From your code, modifying this would work
Solution 4:[4]
You've declared Add()
as a method that accepts 3 arguments: Add(int a, int b, int c)
and you're calling it with none.
Change the method signature from:
private static void Add(int a, int b,int c)
to:
private static void Add()
In addition to that, you'll have to declare a,b,c inside the method:
public static void main(String[] args) {
Add();
}
private static void Add(){
System.out.println("First number...");
int a = input.nextInt();
System.out.println("second number...");
int b = input.nextInt();
int c = a + b;
System.out.println(c);
}
Side note
do not use System.err.
to print debug printings, use System.out.println()
Solution 5:[5]
You can re-write your code like this.
import java.util.Scanner;
public class Math {
static Scanner input = new Scanner(System.in);
private static int Add(int a, int b){
return a + b;
}
public static void main(String[] args) {
System.out.println("First nummber...");
int a = input.nextInt();
System.out.println("secound nummber...");
int b = input.nextInt();
int c = Add(a,b);
System.out.println(c);
}
}
But from your code I can see that you are really new to java perhaps new to programming. So I will recommend you to learn basic java and programming first. You can learn online from
Or can read some books
Solution 6:[6]
ADD(1,2,3) would work ADD() will not
you have declared it with 3 arguments and you are providing it with none
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | practice2perfect |
Solution 4 | |
Solution 5 | mirmdasif |
Solution 6 | Biscuit128 |