-8

I'm trying to make some sort of a primitive form on what I'd optimize to be an interactive BMI calculator I guess. So this is my code.

CODE:

import java.util.Scanner;

class bmi{

public static void main(String args[]) {

Scanner userinput = new Scanner(System.in);

double weight, height, bmi;
String sex;

System.out.println("Hello there!");

System.out.println("I am your Body-Mass index calculator!");

System.out.println("Would you please be so kind and tell me your weight? In kilograms if possible, I'm from Europe xD.");
weight = userinput.nextDouble();

System.out.println("Alrighty, now tell me, how tall are you? In meters this time.");
height = userinput.nextDouble();

System.out.println("Finally, can you tell me wether you are a boy or a girl, if that isn't too intrusive... *wink*");
sex = userinput.nextLine();

bmi = weight / (height * height);

if (sex = "boy")  {
    System.out.println("Thank you for providing me this information, bro!");
    System.out.println("                                    ");
    System.out.println("Hmm, it looks like your results came...");
}
else {
    System.out.println("Thanks for the indormation, sys! ;)");
    System.out.println("                                    ");
    System.out.println("Hmm, it looks like your results came...");
}

   System.out.println("                                           ");

   if (bmi <= 18.5 && sex = "boy")  {
       System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");

   }
   else if (bmi <= 25 && sex = "boy")  {
       System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);

   }
   else if (bmi <= 18.5 && sex = "girl")  {
       System.out.println("Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "bmi",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");

   }
   else if (bmi <= 25 && sex = "girl")  {
       System.out.println("Damn giiiirl, you rock! Having a BMI of "bmi", you are an ideal specimen of a healthy female figure. Keep it up!);

   }
   else if (bmi > 25 && sex = "girl")  {
       System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.);

   }
   else if (bmi > 25 && sex = "boy")  {
       System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.);   

   } 
   else if (bmi > 30) then {
       System.out.println("I am so sorry to be the one to inform you, but with a BMI of "bmi",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3");
   }

   System.out.println("                                           ");
   System.out.println("Thank you for letting me help!");

}

}

OK, and now here is the report:

javac JavaTutorial.java

JavaTutorial.java:39: error: ')' expected

       System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");

                                                                                                                                   ^

JavaTutorial.java:39: error: not a statement

       System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");

                                                                                                                                      ^

JavaTutorial.java:39: error: ';' expected

       System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");

                                                                                                                                                                                                                        ^

JavaTutorial.java:43: error: ')' expected

       System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);

                                                        ^

JavaTutorial.java:43: error: unclosed string literal

       System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);

                                                                                            ^

JavaTutorial.java:43: error: ';' expected

System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);

                                                                                                                                                         ^

JavaTutorial.java:47: error: ')' expected

System.out.println("Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "bmi",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");

                                                                                                                            ^

JavaTutorial.java:47: error: not a statement

System.out.println("Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "bmi",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");

                                                                                                                               ^

JavaTutorial.java:47: error: ';' expected

System.out.println("Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "bmi",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");

                                                                                                                                                                                                                                                                                                                                           ^

JavaTutorial.java:51: error: ')' expected

System.out.println("Damn giiiirl, you rock! Having a BMI of "bmi", you are an ideal specimen of a healthy female figure. Keep it up!);

                                                                    ^

JavaTutorial.java:51: error: unclosed string literal

System.out.println("Damn giiiirl, you rock! Having a BMI of "bmi", you are an ideal specimen of a healthy female figure. Keep it up!); ^ JavaTutorial.java:51: error: ';' expected

System.out.println("Damn giiiirl, you rock! Having a BMI of "bmi", you are an ideal specimen of a healthy female figure. Keep it up!); ^ JavaTutorial.java:55: error: ')' expected

System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.);

                                                                                          ^

JavaTutorial.java:55: error: unclosed string literal

System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.); ^ JavaTutorial.java:55: error: ';' expected

System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.); ^ JavaTutorial.java:59: error: ')' expected

System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.); ^ JavaTutorial.java:59: error: unclosed string literal

System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.); ^ JavaTutorial.java:59: error: ';' expected

System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.);

                                                                                                                                                                      ^

JavaTutorial.java:62: error: not a statement

   else if (bmi > 30) then {

                      ^

JavaTutorial.java:62: error: ';' expected

else if (bmi > 30) then { ^ JavaTutorial.java:62: error: 'else' without 'if'

else if (bmi > 30) then { ^

JavaTutorial.java:63: error: ')' expected

System.out.println("I am so sorry to be the one to inform you, but with a BMI of "bmi",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3"); ^ JavaTutorial.java:63: error: not a statement

System.out.println("I am so sorry to be the one to inform you, but with a BMI of "bmi",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3");

                                                                                            ^

JavaTutorial.java:63: error: ';' expected

System.out.println("I am so sorry to be the one to inform you, but with a BMI of "bmi",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3"); ^ JavaTutorial.java:58: error: 'else' without 'if'

   else if (bmi > 25 && sex = "boy")  {


   ^

JavaTutorial.java:54: error: 'else' without 'if'

   else if (bmi > 25 && sex = "girl")  {


   ^

JavaTutorial.java:46: error: reached end of file while parsing

   else if (bmi <= 18.5 && sex = "girl")  {


   ^

JavaTutorial.java:72: error: reached end of file while parsing

}

^

28 errors

Jan Husak
  • 7
  • 1
  • Consider what is on-topic for this forum: https://stackoverflow.com/help/on-topic – tkruse Dec 04 '17 at 01:07
  • Looking again at your code, you've got multiple errors, and this suggests that your style of coding perhaps needs to be fixed. Don't type a large amount of code and then test it. Instead if you can't use a modern IDE such as NetBeans or Eclipse (which warns you of compilation issues almost immediately), then it is up to you to compile your code **early and often**, and most importantly **to not add any new code until current compilation issues are fixed**. Else you'll end up with a rat's nest of hard to fix errors. – Hovercraft Full Of Eels Dec 04 '17 at 01:07
  • I suggest that you start over – Hovercraft Full Of Eels Dec 04 '17 at 01:07
  • If you have 28 errors and you can't figure out _any_ of them, then you need more help than we can give you. But I suspect that you just panicked and haven't tried to figure them out yet. Look at them one at a time. Start with the first. Sometimes fixing an error will cause more than one error to go away. – ajb Dec 04 '17 at 01:09
  • 1
    Also see https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – ajb Dec 04 '17 at 01:10

1 Answers1

0

I fixed it for you. There were several mistakes.

  1. if comparisons always need two equal signs like this: if(a == b){...}
  2. There is no then in if clauses in java
  3. Always comare Strings with the String.equals method. A String is saved as an object in java. Comparing two objects with == never returns true, unless they are two variables pointing at the same instance. In this case you are comparing the content of an object, which is why you should use string1.equals(string2)
  4. Idk why, but scanner.nextLine() didn't work, but scanner.next() did. Since you are asking for a single word, this shouldn't cause any problems. I'm not too familiar with scanner but it worked for me ;)
  5. when trying to insert a variable into a String you need to use plus signs like this: "something written here " + someVariable + "something written over there".

Here is the fixed code:

    public static void main(String[] args) {

        Scanner userinput = new Scanner(System.in);

        double weight, height, bmi;
        String sex;

        System.out.println("Hello there!");

        System.out.println("I am your Body-Mass index calculator!");

        System.out.println(
            "Would you please be so kind and tell me your weight? In kilograms if possible, I'm from Europe xD.");
        weight = userinput.nextDouble();

        System.out.println("Alrighty, now tell me, how tall are you? In meters this time.");
        height = userinput.nextDouble();

        System.out.println(
            "Finally, can you tell me wether you are a boy or a girl, if that isn't too intrusive... *wink*");
        sex = userinput.nextLine();

        bmi = weight / (height * height);
        if ( sex.equals("boy")) {
            System.out.println("Thank you for providing me this information, bro!");
            System.out.println("                                    ");
            System.out.println("Hmm, it looks like your results came...");
        } else {
            System.out.println("Thanks for the information, sys! ;)");
            System.out.println("                                    ");
            System.out.println("Hmm, it looks like your results came...");
        }

        System.out.println("                                           ");

        if (bmi <= 18.5 && sex.equals("boy")) {
            System.out.println(
                "Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "
                + bmi
                + ") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");

        } else if (bmi <= 25 && sex.equals("boy")) {
            System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of " + bmi
                + ", you are just about average, nothing to worry about here!");

        } else if (bmi <= 18.5 && sex.equals("girl")) {
            System.out.println(
                "Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "
                + bmi
                + ",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");

        } else if (bmi <= 25 && sex.equals("girl")) {
            System.out.println("Damn giiiirl, you rock! Having a BMI of " + bmi
                + ", you are an ideal specimen of a healthy female figure. Keep it up!");

        } else if (bmi > 25 && sex.equals("girl")) {
            System.out.println("First of all, let me say it's not critical, but with a BMI of " + bmi
                + ", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.");

        } else if (bmi > 25 && sex.equals("boy")) {
            System.out.println("First of all, let me say it's not critical, but with a BMI of " + bmi
                + ", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.");

        } else if (bmi > 30) {
            System.out.println("I am so sorry to be the one to inform you, but with a BMI of " + bmi
                + ",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3");
        }

        System.out.println("                                           ");
        System.out.println("Thank you for letting me help!");

    }
Naeramarth
  • 112
  • 13