Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Question: Java coding wont compile

Options
  • 30-01-2012 6:25pm
    #1
    Registered Users Posts: 1,704 ✭✭✭


    Hi everyone, can someone advise me how to go about fixing this code.
    import java.util.Scanner;
    /**
     * Write a description of class Question1 here.
     * 
     * @author Sean Doyle
     * @version Version 1.0, 30/01/2012
     */
    public class Question1
    { 
        public static void main(String [] args)
        {
            System.out.print('\u000C');// Clears the screen
            double height, weight, bmi; // Declare Variables
            
            Scanner input = new Scanner(System.in); // creates a new instance of scanner called input
            
            System.out.println("Welcome, this programme calculates you BMI based on you weight and height");// Welcomes user and informs them of programmes function
            System.out.println("To begin please enther you weight in pounds: ");weight = input.nextDouble();// Prompts user for input, assigns values entered to "weight"
            if(weight>0)// If statement ensures only positave values are entered, also stopes user from entering 0 as a value
            {
                System.out.println("Now please enter your height in inches: ");height = input.nextDouble();// Prompts user for input, assigns values entered to "height"
                if(height >0) //If statement ensures only positave values are entered, also stopes user from entering 0 as a value
                {
                   bmi = calculate_BMI(height, weight);// Calles method calculate_BMI, passes it the variables height and weight. Assigns the returned value to "bmi".
            
                   System.out.print('\u000C');// Clears the screen
                   
                   if(bmi < 16.5) 
                    {
                        returnBMI(); 
                         System.out.print("You are underweight");
                    }
            
                    else if((bmi >= 16.5) && (bmi <= 18.4))
                    {
                       returnBMI();	           
                    }
            
                    else if((bmi >= 18.5) && (bmi <= 24.9))
                    {
                        returnBMI();
                        System.out.print("You are normal");           
                    }
            
                    else if((bmi >= 25) && (bmi <= 29.9))
                    {
                       returnBMI();	
                      System.out.print("You are normal");       
                    }
            
                    else if((bmi >= 30) && (bmi <= 34.9))
                    {
                        returnBMI();	           
                    }
            
                    else if ((bmi >= 35) && (bmi <= 39.9))
                    {
                        returnBMI();
                        System.out.print("You are overweight");	           
                    }
                    else
                    {
                        returnError();
                        System.out.print("You are overweight");
                    }
            
                }
                else//If a non positve value or 0 is entered 
                {
                    returnError();// Calls error method
                }
            }
            else//If a non positve value or 0 is entered 
            {
                returnError();// Calls error method
            }
        }
           
        private static double calculate_BMI(final double height, double weight)// Method for calculate bmi, takes in two variables, height and weight
        {
             return ((weight * 709)/(height*height));// Calculates the BMI using this formula and returns it to the main method
        }
        
        private static void returnError()// Error message method
        {
            System.out.println("Your have entered and incorrect value, please restart the programme");// informs the user they have entered an invalid number.
        }
        
        private static void returnBMI()
        {
            System.out.println("Your height is: " + height + " and you weight is: " + weight);System.out.printf("This gives you a Boday Mass Index of: %.2f" + bmi); // Returs the entered values along with the bmi
        }
    }
    

    My problem is I cant call the returnBMI() method because I get the error, "cannot find symbol -variable height" . I assume this is because height is declared in the main method and not in the class as a whole. I have tried to declare it outside the main method but I get the error "non-static variable, cannot be referenced in a static context". Any ideas how I can do this, I could just put the response in each if statement but Im trying to get it as clean looking as possible. Thanks alot


Comments

  • Registered Users Posts: 211 ✭✭CrazyFish


    You declare the variables inside of the main method which means that these variables have local scope. Which is a fancy way of saying that they can only be accessed in their function/method aka the main method. You should also consider using a constructor so that you don't need to set every method to static (you could even pass the weight and height values as parameters for the constructor and set them so they can be used in the rest of the program.


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Crazyfish is right. Quick fix, change returnBMI() to returnBMI(double height, double width) and pass the local variables in the calling function.

    The bmi variable in returnBMI is superfluous. You just need to run calculateBMI on the two variables and output at runtime.


Advertisement