Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: [BEGINNER] Method not working

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [BEGINNER] Method not working

    public double calculateBMI(double BMI)
    	{
     
    		BMI = (double)startingWeightKG/heightMeters*heightMeters;
    		return BMI;
     
    	}
     
     
     
    	public String toString()
        {
            return "\nName: " + name
            	 + "\nGender: " + gender
                 + "\nID: " + memberID
                 + "\nAddress: " + memberAddress
                 + "\nHeight(In Meters): " + heightMeters
                 + "\nStarting Weight(In kgs): " + startingWeightKG
                 + "\nBMI of " + BMI + " (" + BMIcategory + ").";
        }

    There is obviously a lot more to this than what I posted here, but I think the problem is me not calling the method properly?

    After the print out, it gives a BMI of 0.0, everytime, no matter what the height/weight.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [BEGINNER] Method not working

    it gives a BMI of 0.0
    What are the values of the variables used in the computation: startingWeightKG, heightMeters and heightMeters?
    Add a print statement first thing in the method that prints the values of those variables.

    Why is the value in BMI passed to the method? It is not used.

    What is done with the value returned by the method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [BEGINNER] Method not working

    Quote Originally Posted by Norm View Post
    What are the values of the variables used in the computation: startingWeightKG, heightMeters and heightMeters?
    The member enters them in directly:

    System.out.print("Please enter your height (in meters, between 1m and 3m): ");
    	    	double height = input.nextDouble();
    	    	System.out.print("Please enter your weight (in kg, between 35kg and 250kg): ");
    	    	double weight = input.nextDouble();



    Quote Originally Posted by Norm View Post
    Why is the value in BMI passed to the method? It is not used.
    Because I want to use the value of BMI in the toString method

    Quote Originally Posted by Norm View Post
    What is done with the value returned by the method?
    It's put into the toString method and printed in my main class

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [BEGINNER] Method not working

    Because I want to use the value of BMI in the toString method
    The variable BMI is defined here as local to the method:
    public double calculateBMI(double BMI)
    Its value is never used in the method and could be removed like this:
    public double calculateBMI()

    It's put into the toString method and printed in my main class
    Can you post the code that shows what is done with the value returned by the method?
    There should be an assignment statement:
      someVar = someMethod();  // save value returned by method

    Did you ever do what I suggested:
    Add a print statement first thing in the method that prints the values of those variables.
    What was printed?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java beginner code not working in Ireport :(
    By jakehngo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 28th, 2017, 05:19 PM
  2. Beginner Java Method
    By Johnathanrs in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 25th, 2014, 08:13 PM
  3. Complete beginner (college student) - code not working!!
    By javanewbie085 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2013, 09:19 AM
  4. Help with beginner mutator method
    By Dysun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 06:32 PM
  5. contains method not working?
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2011, 01:57 PM