I just want to know how to call methods/functions in java. Can you please help me with this?
So here's my code.
import java.util.Scanner;
public class MyFirstProject {
public static void main(String[] args) {
hello();
}
static void hello(int a, int b) {
Scanner scan = new Scanner(System.in);
int total;
System.out.print("Enter first number: ");
a = scan.nextInt();
System.out.print("Enter second number: ");
b = scan.nextInt();
total = a + b;
System.out.println("The total is: " + total);
}
}
You make the method hello static for the class MyFirstProject so it has connotations -> Explanation.
But the problem here is that you are missing to pass parameters to the method:
int example_arg_one = 3;
int example_arg_two = 5;
MyFirstProject.hello(example_arg_one,example_arg_two);
2 clicks away, here's a pictured answer - http://www.wikihow.com/Call-a-Method-in-Java
In your code what you're actually missing is passing the required parameters - the values for a and b. The call should actually look like MyFirstProject.hello(2, 5)
Since your method hello(int a, int b) has a parameter of two integers, you need to give it the integers when calling it in order for it to work. But that also doesn't make sense since you have a Scanner which defines your integers in your method. Just remove the parameters of your method and your code should work.
public static void main(String[] args) {
hello();
}
static void hello() {
Scanner scan = new Scanner(System.in);
int total;
System.out.print("Enter first number: ");
int a = scan.nextInt();
System.out.print("Enter second number: ");
int b = scan.nextInt();
total = a + b;
System.out.println("The total is: " + total);
}
Regarding how to call methods, you're doing it right. Just do not neglect your parameter, if your method has one you have to give it one. If you do not know what a parameter is, it is the hello (int a, int b). Your method is expecting you to give it two integers, because that is how you defined your method, you defined it to take two integers. If you want to call it using the parameter, call it in your main and give it two integers, for example hello(1, 2)
Note: If you want to do that you have to remove the scan.nextInt() from your code.
You forgot to pass two parameters a and b into method hello.
public static void main(String[] args) {
int a = 1;
int b = 2;
hello(a,b);
}
Related
Hello
I'm new to java and need someone to answer a problem I'm having. I have recently started a project to make a calculator in Java. However i'm having a problem with one prat of my code. Basically i can't call a string off from an method. Ive tried varoius other attemps to fix the problem but to no avail. Here is the code:
package CalculatorCore;
import java.util.Scanner;
public class calculations {
static void firstNumber() {
Scanner firstNum = new Scanner(System.in);
System.out.print("First Number: ");
String n1 = firstNum.next(); //You can see, i put the string in a method
}
static void secondNumber() {
Scanner secondNum = new Scanner(System.in);
System.out.print("Second Number: ");
String n2 = secondNum.next(); //Here too
}
public static void main(String[] args) {
System.out.println("Please Choose one of the following equasions: +, -, * or /");
System.out.println("");
System.out.println("");
mathEquasions();
}
static void mathEquasions() {
Scanner equasions = new Scanner(System.in);
System.out.print("Enter input: ");
String e = equasions.next();
if (e.equals("+")) {
System.out.println("");
System.out.println("Please enter the first number that you want to add");
firstNumber();
System.out.println("");
System.out.println("Now add the second number");
secondNumber();
var plusAnswer = (n1 + n2); /*The problem is situated here, i need to call the
strings from another class*/
System.out.println("");
System.out.println("");
System.out.println("Your answer is...");
firstNumber();.n1
}
}
I've already used methods to make the user inputs compact so if theres no other way should i remove the methods?
You need to return the numbers you retrieved in your both methods. Don't forget to parse them as integers, using nextInt:
public class calculations {
static int firstNumber() {
Scanner firstNum = new Scanner(System.in);
System.out.print("First Number: ");
int n1 = firstNum.nextInt();
return n1;
}
static int secondNumber() {
Scanner secondNum = new Scanner(System.in);
System.out.print("Second Number: ");
int n2 = secondNum.nextInt();
return n2;
}
}
Then, when calling firstNumber or secondNumber, create new variables to store their return values:
public class calculations {
static void mathEquasions() {
Scanner equasions = new Scanner(System.in);
System.out.print("Enter input: ");
String e = equasions.next();
if (e.equals("+")) {
System.out.println("");
System.out.println("Please enter the first number that you want to add");
int n1 = firstNumber();
System.out.println("");
System.out.println("Now add the second number");
int n2 = secondNumber();
var plusAnswer = (n1 + n2);
System.out.println("");
System.out.println("");
System.out.println("Your answer is...");
}
}
}
welcome to SO! When trying something for the first time it's a good practice to make it as simple as you can, and from there gradually use more complex techniques.
In this case everything is in one class already, so as a first step you could try to put all your code back into the main method.
public static void main(String[] args) {
//All your code can come here first in the order they are supposed to to be called.
}
As a second step, when it all works, you can extract the parts where you would duplicate code, into separate methods.
Like here instead of having firstNumber() and secondNumber() you could have just one, with something like:
static int getNumber() {
Scanner scanner = new Scanner(System.in);
System.out.print("Number: ");
int number = scanner.nextInt();
return number;
}
and you can call the same method to get both numbers:
public static void main(String[] args) {
//...
System.out.println("Please enter the first number that you want to add");
int n1 = getNumber(); // Using the same method for both
System.out.println("");
System.out.println("Now add the second number");
int n2 = getNumber(); // Using the same method for both
//...
}
Learning by doing and jumping into the thick of it is one of the best ways to learn. There are tons of good quality materials freely available (eg. on yt) and they can really boost your skills. That's also how I started learning, so good luck!
I'm having a hard time with my second method, The method declaration is:
public static void displayOutput(int loopCount)
The method is called from the main() and is passed the valid input value which determines repetition. The method displays the output pattern only and returns nothing. Every 3rd line displays a space and 3 asterisks
I know I'm not calling each method right in the main() and I know that displayOutput(int loopCout) is wrong.
Could someone explain this to me or use an example that would help write the program?
public static void main(String[] args) {
int repeat;
Scanner goGet = new Scanner(System.in);
repeat = getValidValue(goGet); //Uncompilable source code -Erroneous sym type
displayOutput(repeat);
}
public static int getValidValue() {
int input;
do {
Scanner getInfo = new Scanner(System.in);
System.out.print("Enter an integer Greater than zero: --> ");
input = getInfo.nextInt();
} while (input <= 0);
return input;
}
public static int displayOutput(int loopCount) {
int i;
for (i = 0; i < loopCount; i++) {
System.out.print("The semester is ending soon. ");
System.out.print("The semester is ending soon. ");
System.out.print("The semester is ending soon.*** ");
}
return loopCount;
}
You are passing a value to method getValidValue which doesn’t take any value.
Also displayOutput is returning loopcount but you are not catching it anywhere so after asterisk it is not displaying anything.
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);
}
}
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.
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
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);
}
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.
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
How to get an user input using scanner class and use it inside method to process, and obtain output?
Example : You get two numbers from the user and pass it to 4 methods to get its value after addition, subtraction, multiplication, division.
The scanner should be in main class and not inside every method.
Here is my code, it's in basic form. I want to get input from the user and process it inside the methods and get the result:
class Calc{
public static void main(String[] args){
add();
sub();
mul();
div();
{
void Add() {
int a=5,b=10;
sum=a+b;
System.out.println(sum);
}
void sub() {
int a=5,b=10;
sub=a=b;
System.out.println(sub);
}
void mul() {
int a=5,b=10;
mul=a*b;
System.out.println(mul);
}
void div() {
int a=5,b=10;
div=a/b;
System.out.println(div);
}
}
}
}
Simply use the array or nextInt() method to get the number of input and passed it to your method.
For e.g. If you are supposed two take two input at time
Scanner sc = new Scanner(System.in);
int[] integers = new int[2];
for(int i = 0; i < 2; i++)
{
integers[i] = sc.nextInt();
}
Now use the integers array to get the value from index and passed it to your method
You can get input by using new Scanner(System.in), as follows:
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
add(a,b);
sub(a,b);
multiply(a,b);
divide(a,b);
I want to make sum of two numbers. But I have problems to do that. I don't understand why my sum is always zero.
import java.util.*;
public class Numbers {
static int a;
static int b;
static int result;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Type the first number:");
String a = in.nextLine();
System.out.println(a);
System.out.println("Type the second number:");
String b = in.nextLine();
System.out.println(b);
display();
}
public static void display(){
result=a+b;
System.out.println("Sum of numbers is " + result);
}
}
I'm not a java programmer, but I can see that you have global variables called a and b and local variables called a and b.
Your main() is setting the local variables. display() is reading the global variables.
You don't need static "global" variables in your case. So you can remove this piece of code :
static int a;
static int b;
static int result;
Then, juste use your local variables and give a and b as parameter to display() method like this :
public static void display(int a, int b){
System.out.println("Sum of numbers is " + (a+b));
}
Eventually, you will need to cast Strings obtained via in.nextLine() and cast them to int, with Integer.parseInt() for example. Or just directly get integer from the user.
You did couple of mistakes :
String a and String b is a local variable to main() method. So you cant access
those variables from display() method. result=a+b;in display() method is actually adding a and b which are declared in the class level whose by default value is 0. so summation is 0.
Even if you write result=a+b;inside main method it will not work. Because a and b are String in main method and you cant do addition on String
Use this code
import java.util.*;
public class NewClass {
static int a;
static int b;
static int result;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Type the first number:");
a = in.nextInt();
System.out.println(a);
System.out.println("Type the second number:");
b = in.nextInt();
System.out.println(b);
display();
}
public static void display(){
result=(a+b);
System.out.println("Sum of numbers is " + result);
}
}
Use nextInt in place of nextLine method .If the type is int then use nextInt method.