• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing variable issue within program  RSS feed

 
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, my objective is to write a program that calculates the bodyfat of people. The difficult thing is, that the calculations are different for males and females, so I tried to prompt the user to state whether they are male or female, then use and "if" statement to tether their response to the corresponding calculations.

Here's my algorithm:



Here's what I have:



It keeps telling me that bodyweight is not initialized, but when I do, I get a hell of a lot more bugs on everything telling me they aren't initialized. I just want the program to transfer the user to select inputs, based on whether they are male or female.
 
Greenhorn
Posts: 1
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is you are not initializing the male/female variable. Use the foll before the if statement:
String input = console.nextLine();
if(input.equals("male")) male = true;
else if(input.equals("female")) female = true;
else System.out.println("Enter either male/female");

This should work for you!
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I did that, but still have a bug on line 70 saying to delete the "else" token. But if I do I get more bugs...

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a correct program.
Please check it.
Please make sure that you are using correct formulas for calculation.

 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to run your version, but it just says Wrong input for any input
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dan Good

Now check, code edited at line 18 and 20. It should work.

 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed some things, but now it seems the program only runs the male side and not the female, no matter if I put male, female, true, or false. Only runs male calcs

 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dan Good

Problem is at line 19 and 21. you should use equals and not ==.
what is difference between equals and == in java
As you initialize male variable to true, so program executing only male part.
You should study my program.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I caught my mistake! I appreciate everyone for helping me out! Huge brain buster for me so many thanks, cheers.
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Java World.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaja Shinde wrote:Following is a correct program. . . .
All the programs shown so far have been very incorrect, because they try to do everything in the main method. Cut your main method down to the two lines Winston suggests, and everything else goes elsewhere. If you had used object‑oriented programming (which nobody has even got anywhere near), you would have a much simpler program.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaja Shinde wrote: . . . Java World. . . .
Java World? Are you sure ?
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Pankaja Shinde wrote:Following is a correct program. . . .
All the programs shown so far have been very incorrect, because they try to do everything in the main method. Cut your main method down to the two lines Winston suggests, and everything else goes elsewhere. If you had used object‑oriented programming (which nobody has even got anywhere near), you would have a much simpler program.


Yes, you are right Sir.
We need to move business logic outside of main.
I just try to modify his program to remove his logical / programmatic errors.
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Pankaja Shinde wrote: . . . Java World. . . .
Java World? Are you sure ?


No Sir I didn't mean another forum.
I want to say, welcome to world of java.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaja Shinde wrote: . . .No Sir I didn't mean another forum. . . .
Hahahaha!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!