0

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() {
}
  • What about changing `option()` to `option(int num)`? – Planck Constant Jul 22 '20 at 16:26
  • Unrelated to question but, change `options()` scope to `private` if only accessible within class – Shubham Agrawal Jul 22 '20 at 16:34
  • As you're new to Java. You can achieve it multiple ways. 1) One, is to create member variable and assign read value to it and using it in another method within same scope and 2) Second, is to pass input value to a variable parameter to another method. – Mandy Jul 22 '20 at 16:53
  • 1
    @Mandy Thank you –  Jul 22 '20 at 17:13
  • You might want getNum() to be ``public static int getNum()`` so it could return the number to the calling code. – NomadMaker Jul 25 '20 at 08:39

3 Answers3

1

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;
}
adq
  • 184
  • 7
  • To avoid code duplication, he can call ```option()``` from main context and use ```int getNum()``` method within ```option()``` to read value from input stream. – Mandy Jul 22 '20 at 16:55
1

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) {
    
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rohan Sharma
  • 324
  • 3
  • 12
  • Great explanation and exactly what I needed. Thank you I will study this now. –  Jul 22 '20 at 17:17
-1

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;
    }
}
Mandy
  • 163
  • 1
  • 9