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 2 of 2

Thread: GUI - calculate BMI (need help asap)

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI - calculate BMI (need help asap)

    Hi all, i need to calculate BMI (take 'weight' and divide by 'height' twice) and show in the Advice textbox with the result. I had added in the NumberFormatException to check for user input but I have problem with the lines after it. Please advise how can I eliminate the error. Appreciate the help. Thanks.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
     
    public class BMI extends JFrame
    {
       private JLabel lblHeader;
     
       private JLabel lblHeight;
       private JTextField txtHeight;
       private JLabel lblM;
     
       private JLabel lblWeight;
       private JTextField txtWeight;
       private JLabel lblKg;
     
       private JLabel lblBmi;
       private JLabel lblBmiOutput;
     
       private JLabel lblAdvice;
       private JLabel lblAdviceOutput;
     
       private JButton btnProceed;
       private JButton btnExit;
     
     
        public BMI()
        {
            lblHeader = new JLabel("BMI Advisor");
     
            lblHeight = new JLabel("Height");
            txtHeight = new JTextField(5);
            lblM = new JLabel("m");
     
            lblWeight = new JLabel("Weight");
            txtWeight = new JTextField(5);
            lblKg = new JLabel("kg");
     
            lblBmi = new JLabel("BMI");
            lblBmiOutput = new JLabel();
     
            lblAdvice = new JLabel("Advice");
            lblAdviceOutput = new JLabel();
     
            btnProceed = new JButton("Proceed");
            btnExit = new JButton("Exit");
     
            JPanel panel = (JPanel)getContentPane();
     
            //null layout
            panel.setLayout(null);
            lblHeader.setBounds(120,20,200,30);
            lblHeader.setFont(new Font ("Arial", Font.BOLD, 30));
     
            lblHeight.setBounds(50,70,40,20);
            txtHeight.setBounds(100,70,40,20);
            lblM.setBounds(150,70,40,20);
     
            lblWeight.setBounds(50,100,40,20);
            txtWeight.setBounds(100,100,40,20);
            lblKg.setBounds(150,100,40,20);
     
            lblBmi.setBounds(50,130,40,20);
            lblBmiOutput.setBounds(100,130,40,20);
     
            lblAdvice.setBounds(50,160,40,20);
            lblAdviceOutput.setBounds(100,160,40,20);
     
            btnProceed.setBounds(270,130,90,20);
            btnExit.setBounds(270,160,90,20);
     
            panel.add(lblHeader);
            panel.add(lblHeight);
            panel.add(txtHeight);
            panel.add(lblM);
            panel.add(lblWeight);
            panel.add(txtWeight);
            panel.add(lblKg);
            panel.add(lblBmi);
            panel.add(lblBmiOutput);
            panel.add(lblAdvice);
            panel.add(lblAdviceOutput);
            panel.add(btnProceed);
            panel.add(btnExit);
     
            btnProceed.addActionListener(new MyListener());
            btnExit.addActionListener(new MyListener()); 
     
            setTitle("BMI");
            setSize(400,250);
            setVisible(true);
        }
     
        public double calBmi (double bmi) 
        {
            return bmi;
        }
     
        public class MyListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
     
            if (e.getSource() == btnProceed)
            {
                try
                {   
                    double weight = Double.parseDouble(txtHeight.getText());
                    double height = Double.parseDouble(txtWeight.getText());        
                } catch (NumberFormatException ex)
                {
                    lblAdviceOutput.setText("Invalid input!");
                } 
     
                    //ERROR HERE..
                    double bmi = weight/(2*height);
     
                        if (bmi > 18.5)
                            {
                                lblAdviceOutput.setText("You are underweight.");
                            } 
                        if (bmi >23.0)
                            {
                                lblAdviceOutput.setText("You are overweight.");
                            }
                        else
                            {        
                                lblAdviceOutput.setText("You are normal weight.");
                            } 
     
            }
            else if (e.getSource() == btnExit)
                System.exit(0);
            }   
        }
     
     
     
     
        public static void main (String [] args)
        {
            new BMI();
        }
     
    }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: GUI - calculate BMI (need help asap)

    try
    {
    double weight = Double.parseDouble(txtHeight.getText());
    double height = Double.parseDouble(txtWeight.getText());
    } catch (NumberFormatException ex)
    {
    lblAdviceOutput.setText("Invalid input!");
    }
    variable declared in try block has local scope in the block,these variables cannot be accessed outside the try block, so declare them outside try block.

    double weight = 0.0f;
    double height = 0.0f;
     
                try
                {   
                   height = Double.parseDouble(txtHeight.getText());
                   weight= Double.parseDouble(txtWeight.getText());        
                } catch (NumberFormatException ex)
                {
                    lblAdviceOutput.setText("Invalid input!");
                }
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Bowler calculate project, two errors
    By samerman in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 1st, 2011, 12:14 PM
  2. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  3. Calculate federal taxes program! Help!
    By ocmjt in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 11th, 2010, 11:25 AM
  4. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2009, 03:29 AM
  5. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2009, 12:41 AM