Hey guys,

I'm a newbie programmer and I'm trying to create a program which calculates BMI. Weight is in lbs and height is in inches. I've compiled this and its got quite a bit of errors, so I could really use some help.

The final result that I'm trying to get is for example if a user types in 165 for weight and 70 for height then it should print out

A person with weight 165 lbs and height 70 inches has bmi = 23.672448979591838

Any help is appreciated!

*int type declarations must remain in the program*

This is my program so far:

// HealthIndex.java
// My Name
public class HealthIndex
{
    // interactively reads in an integer height and weight
    // prints out the douvel BMI for the given height and weight
public static void main( String[] args )
    {
    final int BMI_CONSTANT = 703;
    int weight; // in lbs.
    int height; // in inches
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter in your weight: ");
    int weight = scan.nextDouble();

    System.out.println("Enter in your height: " );
    int height = scan.nextDouble();

    // calculate Body Mass Index(bmi) and print results
    weight = pound;
    height = inches;
    int BMI = weight/(height*height);
    
    System.out.println(A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+bmi+"); )

    
    }
}

Cheers,

~Jibby

Member Avatar for coil

Please post your errors. Then it's much easier to find out what the problems are.

Just from looking over it, you need to address the following:
1. Lines 16 & 19: If the user doesn't enter a double, your program will crash. You should probably have some sort of backup, for example forcing the user to re-enter a double.
2. Lines 16 & 19: I'm sure this is a simple typo, but you declare weight and height, which you've already declared.
3. Lines 22 & 23: I don't see where you declare pound and inches, and in any case, you shouldn't need them. You want to calculate the BMI based on the user input, so why would you assign the values to something else?
4. Line 26: You forgot the opening quote ("), which messed up the rest of the statement.

// HealthIndex.java
// My Name
public class HealthIndex
{
    // interactively reads in an integer height and weight
    // prints out the douvel BMI for the given height and weight
public static void main( String[] args )
    {
    final int BMI_CONSTANT = 703;
    int weight; // in lbs.
    int height; // in inches
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter in your weight: ");
    double weight = scan.nextDouble();

    System.out.println("Enter in your height: " );
    double height = scan.nextDouble();

    // calculate Body Mass Index(bmi) and print results

    int BMI = weight/(height*height);
    
    System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+bmi+"); )

    
    }
}

Here are some of the errors:

HealthIndex.java:25: unclosed string literal
System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+bmi+"); )
^
HealthIndex.java:25: ';' expected
System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+bmi+"); )
^
HealthIndex.java:29: reached end of file while parsing
}
^
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
____

and then this (if i remove the last statement)


HealthIndex.java:13: cannot find symbol
symbol : class Scanner
location: class HealthIndex
Scanner scan = new Scanner(System.in);
^
HealthIndex.java:13: cannot find symbol
symbol : class Scanner
location: class HealthIndex
Scanner scan = new Scanner(System.in);
^
HealthIndex.java:16: weight is already defined in main(java.lang.String[])
double weight = scan.nextDouble();
^
HealthIndex.java:19: height is already defined in main(java.lang.String[])
double height = scan.nextDouble();
^
4 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

  • Add import java.util.Scanner; to the top of your program (outside your class).
  • Remove lines 10 and 11.
  • On line 23, change int to double.
  • Line 25 needs some modification, change it from:

System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+bmi+"); ) to: System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+BMI);

Thanks for the help so far, but I'm still having a few issues.

This must remain in the program:

final int BMI_CONSTANT = 703;
    int weight; // in lbs
    int height; // in inches

I removed the second declaration of weight and height as it's not needed. I'm not sure if BMI needs to be declared, does it?

This is my program so far:

// HealthIndex.java
// My Name
public class HealthIndex
{
    // interactively reads in an integer height and weight
    // prints out the douvel BMI for the given height and weight
public static void main( String[] args )
    {
    Scanner scan = new Scanner(System.in);
    
    final int BMI_CONSTANT = 703;
    int weight; // in lbs
    int height; // in inches

    System.out.println("Enter in your weight: ");
    weight = scan.nextDouble();

    System.out.println("Enter in your height: " );
    height = scan.nextDouble();

    // calculate Body Mass Index(bmi) and print results

    double BMI = weight/(height*height);
    
    System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+BMI);
    
    }
}

These are the errors:

HealthIndex.java:9: cannot find symbol
symbol : class Scanner
location: class HealthIndex
Scanner scan = new Scanner(System.in);
^
HealthIndex.java:9: cannot find symbol
symbol : class Scanner
location: class HealthIndex
Scanner scan = new Scanner(System.in);
^
2 errors


Thanks for all the help so far! :)

~Jibby

  • You forgot the import I was talking about in my previous post. No worries, just add the following line to the top of your file: import java.util.Scanner;
  • If variables height and weight are of type int, then this won't work: weight = scan.nextDouble(); You either need to make both variables of type double, or you read an integer value instead of a double value, by writing weight = [B]scan.nextInt();[/B] instead.
  • [NOTE: this only applies if variables weight and height are of type int]

    In this line: double BMI = weight/(height*height); you're effectively assigning an integer value to a variable of type double, this is because an integer division occurs. The result of an integer division is simply: another integer. Probably this is not your intent, and you might want to put in a cast to double to prevent an integer division from occuring, doing a floating point division instead. You can do this by changing that line to: double BMI = [B](double)[/B]weight/(height*height);

weight = scan.nextInt(); instead.


Ahh, that's what I had wrong! The program works! :)

Thank you very much.

I have question though.

Is import java.util.Scanner; similar to Scanner scan = new Scanner(System.in); ?

In which case Scanner scan is not needed, or is it? I'm such a newb, it's just we haven't covered import java.util.Scanner, so I'm thinking there must be a different way to make this work without it?

>>Is import java.util.Scanner; similar to Scanner scan = new Scanner(System.in); ?

No, it's just another way to let the Java compiler know what Scanner class you mean. That is: there are many programmers around, and if a programmer decides to write a class and call it Scanner, then this is possible. But in the Java API there's also a class named Scanner, however it's in a package: java.util. Actually the full name of the Scanner class in this example is java.util.Scanner. Now you've two options if you want to use that class:

Option one -- explicitly typing 'java.util.Scanner', every time you want to use the class
java.util.Scanner.

Option two: -- importing java.util.Scanner, so you do not have to type it every time again, and so you can just refer to java.util.Scanner by typing 'Scanner'. This is the most common way, and it saves you a whole lot of typing.

Hmm, actually it's not working but it's taking shape.

The algorithm to determine BMI isn't functioning properly when data is entered into the program.

// HealthIndex.java
// My Name
import java.util.Scanner; 
public class HealthIndex
{
    // interactively reads in an integer height and weight
    // prints out the douvel BMI for the given height and weight
public static void main( String[] args )
    {
    Scanner scan = new Scanner(System.in);
    
    final int BMI_CONSTANT = 703;
    int weight; // in lbs
    int height; // in inches

    System.out.println("Enter in your weight: ");
    weight = scan.nextInt();

    System.out.println("Enter in your height: " );
    height = scan.nextInt();

    // calculate Body Mass Index(bmi) and print results

    double BMI = weight/(height*height);
    
    System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+BMI);
    
    }
}

>>Hmm, actually it's not working but it's taking shape.
>>The algorithm to determine BMI isn't functioning properly when data is entered into the program.

Well, that's probably because of the integer division that I talked about in this post. However, no worries, if you read this quote, then you'll be able to fix that:

[NOTE: this only applies if variables weight and height are of type int]

In this line: double BMI = weight/(height*height); you're effectively assigning an integer value to a variable of type double, this is because an integer division occurs. The result of an integer division is simply: another integer. Probably this is not your intent, and you might want to put in a cast to double to prevent an integer division from occuring, doing a floating point division instead. You can do this by changing that line to: double BMI = [B](double)[/B]weight/(height*height);

*facepalm*

Thank you so much! Problem solved. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.