user12580773
user12580773

Reputation:

How can I use parameters to pass down a non constant variable value to a method?

I'm struggling with passing a variable value that a user has entered into my program into a method. I think the technical word is parameters.

The problem I'm having is that after the user enters a number in the getnum() method I want the number to be passed down to the two methods calculation and `calculation_two. However, I can't seem to be able to achieve it.

Just to explain the program, the user enters a number in the getnum() method, then they go to the option method and after they select what option, in the calculations methods the number that was written in the getnum() method needs to be passed down the the calculations methods. Therefore, I will then be able to perform calculations with it that way. I need the program to be set up like this as well for my own personal reasons.

Can anyone assist please?

Thanks

public static void main (String[]args){

    getnum();
}

public static void getnum() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = input.nextInt();
    option();
}

public static void option() {
    Scanner input2 = new Scanner(System.in);
    System.out.println("would you like to see option 1 or 2");
    int num2 = input2.nextInt();
    if(num2==1) {calculation();}
    else if(num2==2) {calculation_two();}
    else {System.exit(0);}

}

public static void calculation() {
}
public static void calculation_two() {
}

Upvotes: 0

Views: 285

Answers (3)

Rohan Sharma
Rohan Sharma

Reputation: 326

Please see call by reference and call by value to further clarify how primitives or objects are passed from one method to another.

Here are the steps to pass the parameter from one method to another:

  1. You pass the int you got from the scanner as an argument in the option method call:
    public static void getnum() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int num = input.nextInt();
        option(num);   // You pass the int as an argument
    }
  1. Now, since you are now calling a method with arguments you need to change the signature of the option method to take a parameter. You pass the int value(theNumber variable) from the option method's parameter to the calculation method call.

Arguments and parameters are terms which are used interchangeably but you can check the difference here.

    public static void option(int theNumber) {   // option now takes a int parameter
        Scanner input2 = new Scanner(System.in);
        System.out.println("would you like to see option 1 or 2");
        int num2 = input2.nextInt();
        if(num2==1) {   
           calculation(theNumber);  // method now takes an argument
        }
        else if(num2==2) {
           calculation_two(theNumber);  // method now takes an argument
        }
        else {System.exit(0);}
    }
  1. You pass the parameter again to the calculation(s) method and change the signature to:
    public static void calculation(int theNumber) {  // method with parameter
    
    }
    
    public static void calculation_two(int theNumber) {
    
    }

Upvotes: 1

Mandy
Mandy

Reputation: 173

To answer with secure coding practices

package in.faridabad.mandheer;

public class Main{

    public static void main (String[]args){
        Main m = new Main();
        // Assuming calculation is "sum"
        System.out.println("sum is + "+ m.option());
    }

    private int getnum() {
        Scanner input = new Scanner(System.in);
        return input.nextInt();
    }

    int option() {
        System.out.println("would you like to see option 1 or 2");
        int num2 = getnum();
        if(num2==1) {return calculation();}
        else if(num2==2) {return calculation_two();}
        else {System.exit(0);}
    
    }

    private int calculation() {
        System.out.println("Enter a number: ");
        int num1 = getnum();
        return num1;
    }

    private int calculation_two() {
        System.out.println("Enter a number: ");
        int num1 = getnum();
        System.out.println("Enter 2nd number: ");
        int num2 = getnum();
        return num1+num2;
    }
}

Upvotes: -1

adq
adq

Reputation: 184

Declare your methods to use parameters and return values. Easiest way to do it:

public static void main (String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = input.nextInt();
    System.out.println("would you like to see option 1 or 2");
    int option = input.nextInt();
    int result = 0;
    if (option == 1) {
       result = calculation(num);
    } else if (option == 2) {
       result = calculation_two(num)
    } else {
       System.out.println("Invalid option, exiting...");
       System.exit(0);
    }
    System.out.println("Result = " + result);
}

private static int calculation(int num) {
    // implement this
    return 0;
}
private static int calculation_two(int num) {
    // implement that
    return 0;
}

Upvotes: 1

Related Questions