Stephen
Stephen

Reputation: 29

How do I use the return value from a method in another method different from the calling method?

I'm kinda new to to java and stumbled on a problem that needs me to do currency conversion declaring different methods for: getting amount, getting conversion rate, doing the actual conversion and printing the outcome of the conversion

import java.util.*;

public class Conver {
    public static void main(String[] args){
       amountToConvert();
       exchangeRate(); 
       convert();
    }

        public static double amountToConvert() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the amount you wish to convert...");
        double amount = input.nextDouble();
        return amount;
    }

    public static double exchangeRate(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the currency you wish to convert from... ");
        String initialCurrency = input.next();
        System.out.println("Enter the currency you wish to convert from... ");
        String finalCurrency = input.next();
        System.out.println("How many " + initialCurrency + " makes one " + finalCurrency + "?");
        double rate = input.nextDouble();
        return rate;
    } 

    public static double convert(){
        int x = amount*rate;
        return x;
    }

    public void printResult(){
        System.out.println(x);
    }
}

Upvotes: 2

Views: 110

Answers (3)

Andrea
Andrea

Reputation: 849

First off, you need to change the "receiving method" so that it takes an argument. A method like:

public static double convert() {} 

that needs to take in a value for amount and rate, needs to have those added to the method signature:

public static double convert (double amount, double rate) {}

Putting the two comma separated values inside of the parens means that this method takes two values, doubles, as arguments. This makes those values available to use inside of that method.

Now that you have a method that can take the required arguments, you need to actually use that method in your code. When calling this method, you start out the same as with others:

convert(

but then you need to add in the arguments you are using:

double amount = amountToConvert();
double rate = exchangeRate();
convert(rate, amount);

If you want to avoid creating those two additional variables in main(), you can actually call those methods inside of your new method:

convert(amountToConvert(), exchangeRate());

Upvotes: 1

Kamil Jarosz
Kamil Jarosz

Reputation: 2197

Pass returned values to the method convert:

import java.util.*;

public class Conver {
    public static void main(String[] args){
        double amount = amountToConvert();
        double rate = exchangeRate(); 
        double result = convert(amount, rate);
        printResult(result);
    }

    public static double amountToConvert() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the amount you wish to convert...");
        double amount = input.nextDouble();
        return amount;
    }

    public static double exchangeRate(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the currency you wish to convert from... ");
        String initialCurrency = input.next();
        System.out.println("Enter the currency you wish to convert from... ");
        String finalCurrency = input.next();
        System.out.println("How many " + initialCurrency + " makes one " + finalCurrency + "?");
        double rate = input.nextDouble();
        return rate;
    } 

    public static double convert(double amount, double rate){
        double x = amount * rate;
        return x;
    }

    public void printResult(double x){
        System.out.println(x);
    }
}

Also, don't use double for money!

Upvotes: 1

deezy
deezy

Reputation: 1480

Learn to use parameters in the methods. Change the convert() method so that it looks like this:

public static double convert(double amount, double rate){
    int x = amount*rate;
    return x;
}

In the method above, double amount and double rate are the parameters. Use variables to help pass in parameters to convert() in the main method:

public static void main(String[] args){
   double amount1 = amountToConvert();
   double rate1 = exchangeRate(); 
   double result = convert(amount1, rate1);
   printResult(result);
}

Hope this helps!

Upvotes: 1

Related Questions