2

I am trying to use the variables from a static method named getBMI:

public static double getBMI(int weightKG, int heightCM)
{
    Scanner input = new Scanner(System.in);
    // Input Weight
    System.out.print("Enter your weight in kilograms: ");
            weightKG = input.nextInt();
    System.out.println();
    // Input Height
    System.out.print("Enter your height in centimeters: ");
    heightCM = input.nextInt();
    System.out.println();
            return heightCM;
            return weightKG;
}

And use it in another static method named calculateMetricBMI:

public static void calculateMetricBMI()
{
    getBMI();
    System.out.println("A body mass index of 20-25 is considered \"normal\"");
    double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
    System.out.print("Your BMI is " + bmiMetric);
} 

However, I am getting an error when attempting to getBMI(); in the calculateMetricBMI.

EDIT:

Decided to add parameters to getBMI(); Now it shows getBMI(int weightKG, heightCM);

However I get this error:

'.class' expected

';' expected

';' expected

unexpected type required: value found: class

  • What is exactly the error that you're getting, Could you post it on the question please? – Elian Kamal Sep 27 '15 at 22:50
  • Methods can't return values twice. Also you should probably store somewhere result returned by methods. – Pshemo Sep 27 '15 at 22:50
  • 1
    This link may help. http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method – Paul Boddington Sep 27 '15 at 22:55
  • Write one method to read each of the inputs and return a single value. These methods should take a Scanner. Then use the results when calculating the value, optionally passing them to a discrete calculation function. There is no warrant for static variables (which are not present here). – user2864740 Sep 27 '15 at 22:57
  • If you're going to have people do your homework for you, at least select an answer. – Arthur Sep 28 '15 at 01:23

3 Answers3

1

You're getting an error because you're calling getBMI() with nothing in the parameters, it needs to take 2 ints to be called.

You also can't return 2 variables from 1 method.

Try this:

public static void calculateMetricBMI() {
    double weightKG = getWeight();
    double heightCM = getHeight();

    System.out.println("A body mass index of 20-25 is considered \"normal\"");
    double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
    System.out.print("Your BMI is " + bmiMetric);
} 

public static double getWeight() {
    Scanner input = new Scanner(System.in);
    // Input Weight
    System.out.println("Enter your weight in kilograms: ");
    double weightKG = input.nextInt();

    return weightKG;
}

public static double getHeight() {
    Scanner input = new Scanner(System.in);
    // Input Height
    System.out.println("Enter your height in centimeters: ");
    double heightCM = input.nextInt();

    return heightCM;
}

And in your main just call

    calculateMetricBMI();

This is a pretty redundant solution, you can always just ask for the input in calculateMetricBMI() rather than having to call 2 other methods.

Arthur
  • 2,622
  • 4
  • 28
  • 46
0
  1. You can not return two values at the same time in a method. However, I changed your code to return directly the result of your calculation.

  2. Your method doesn't need parameters if you're going to initialize them inside of it. Just create them inside of your method.

  3. If you're willing to use a method with parameters, when you call the method, you should of course do it using parameters

getBMI() will not work here, you should've called getBMI(50.0, 165.0) for example but this is a bad example cause it wouldn't have worked anyway.

Look at the code below, it is much clearer and will get you a better result.

public static double getBMIMetric()
{
    Scanner input = new Scanner(System.in);
    // Input Weight
    System.out.print("Enter your weight in kilograms: ");
    int weightKG = input.nextDouble();
    System.out.print("Enter your height in centimeters: ");
    int heightCM = input.nextInt();
    System.out.println();
    return weightKG/Math.pow(heightCM/100.0, 2);
}

public static void calculateMetricBMI()
{
    System.out.println("A body mass index of 20-25 is considered \"normal\"");
    double bmiMetric = getBMIMetric();
    System.out.print("Your BMI is " + bmiMetric);
} 
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

You need to call getBMI(); with two paremters.

Also, you cannot return 2 different variables, although that would probably result in a syntax error.

Another thing to watch out for is that you need to save what getBMI(); is sending you.

Such as: int weightKG = getBMI(), and getBMI() should only return one value, not two as you describe in your code.

Rubydesic
  • 3,386
  • 12
  • 27