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

Thread: having a little trouble

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

    Default having a little trouble

    I am a student and have an assignment due, I have been trying to figure out this issue for the past 3 days and have yet to get this to work properly without errors. Is there anyone who can help me fix this?
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class AccurateBMI extends JComponent implements ActionListener {
        JFrame frame = new JFrame("A More Accurate BMI Calculator");
        Container content = frame.getContentPane();
        JLabel male = new JLabel();
        JTextField trueFalse = new JTextField();
        JLabel weight = new JLabel();
        JTextField heavy = new JTextField();
        JLabel height = new JLabel();
        JTextField inches = new JTextField();
        JLabel wrist = new JLabel();
        JTextField circum = new JTextField();
        JButton calcBMI = new JButton();
        JLabel totalBMI = new JLabel();
     
        public static void main(String[] args) {
            AccurateBMI application = new AccurateBMI();   
        }
        public void actionPerformed(ActionEvent event) {
            String gender = trueFalse.getText();
            double pounds = Double.parseDouble(heavy.getText());
            double height = Double.parseDouble(inches.getText());
            double wrist = Double.parseDouble(circum.getText());
            double num1 = height*height;
            double bmi = (pounds/num1)*703;
            totalBMI.setText("Your more accurate BMI is: " + bmiTot); //This is where 1 error is, the bmiTot
            //totalBMI.setText("Your more accurate BMI is: " + );
            if (gender == T | gender == t){ //the other 2 errors are both "t" and "T". I've tried string, char and boolean
                if (height >= 65 && wrist >= 7.5){
                    double bmiTot = bmi*.9;
                }else   {
                    double bmiTot = bmi;
                }                
                }else   {
                    if (height >= 65 && wrist >= 6.5 || height > 62 && height < 65 && wrist >= 6.25 || height <= 62 && wrist > 5.75){
                        double bmiTot = bmi*.9;
                }else   {
                        double bmiTot = bmi;
                    }
            }
     
        }
        public AccurateBMI()  {       
            content.setBackground(Color.white);
            content.setLayout(null);
            content.setForeground(Color.yellow);
            content.setFont(new Font("Arial", Font.ITALIC, 14));
            content.add(this);
     
            male.setText("You are a male. T or F: ");
            male.setLocation(5,5);
            male.setSize(300,30);
            content.add(male);
     
            trueFalse.setText("");
            trueFalse.setLocation(310,5);
            trueFalse.setSize(50,30);
            content.add(trueFalse);
            trueFalse.addActionListener(this);
     
            weight.setText("How much do you weigh in pounds?");
            weight.setLocation(5,50);
            weight.setSize(300,30);
            content.add(weight);
     
            heavy.setText("");
            heavy.setLocation(310,50);
            heavy.setSize(50,30);
            content.add(heavy);
            heavy.addActionListener(this);
     
            height.setText("Please enter your height in inches: ");
            height.setLocation(5,95);
            height.setSize(300,30);
            content.add(height);
     
            inches.setText("");
            inches.setLocation(310,95);
            inches.setSize(50,30);
            content.add(inches);
            inches.addActionListener(this);
     
            wrist.setText("How many inches around is your wrist? ie: 7.25 ");
            wrist.setLocation(5,140);
            wrist.setSize(300,30);
            content.add(wrist);
     
            circum.setText("");
            circum.setLocation(310,140);
            circum.setSize(50,30);
            content.add(circum);
            circum.addActionListener(this);
     
            calcBMI.setText("Calculate");
            calcBMI.setLocation(130,180);
            calcBMI.setSize(100,30);
            content.add(calcBMI);
            calcBMI.addActionListener(this);
     
            totalBMI.setText("  ");
            totalBMI.setLocation(130,220);
            totalBMI.setSize(150,30);
            content.add(totalBMI);        
     
            frame.setLocation(0,0);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(385,255);
            frame.setVisible(true);
    }
    }

    netbeans is the IDE we have to use for the class
    Last edited by XSmoky; November 6th, 2011 at 03:28 PM. Reason: clarification


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: having a little trouble

    Please explain what the problem is. post any error messages you get.
    Show the program's output and explain what is wrong and show what it should be.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: having a little trouble

    this is the list of errors, the window opens, i enter the information, click the button and this is the result. what it should do instead of all this is to calculate the information and post the result

    the problems are on lines 29 and 31 if you copy and paste the entire thing to an ide


    run:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at AccurateBMI.actionPerformed(AccurateBMI.java:35)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6504)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:626 9)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713 )
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101 )
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
    BUILD SUCCESSFUL (total time: 26 seconds)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: having a little trouble

    Uncompilable source code - Erroneous tree type: <any>
    at AccurateBMI.actionPerformed(AccurateBMI.java:35)
    What code is at line 35 in AccurateBMI.java?

    Can you compile your program and get errors from the compiler? The IDE seems to hide what the error is.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: having a little trouble

    line 29 = line 35 - comments

    line 29(35): totalBMI.setText("Your more accurate BMI is: " + bmiTot); //error part underlined

    line 31(37): if (gender == T | gender == t){ //problems underlined

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: having a little trouble

    What are the errors?
    Common errors would be the variables are NOT defined.
    Is bmiTot defined? And is its definition in scope where you are using it?

    gender is a String. You need to use the equals() method to test if its contents is equal to the contents of the T variable and the t variable.
    Are T and t variables? Where are they defined? Should the be Strings? If so put them in "s

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: having a little trouble

    This is editted and fixed one problem but created another

     public void actionPerformed(ActionEvent event) {
            String gender = trueFalse.getText();
            double pounds = Double.parseDouble(heavy.getText());
            double height = Double.parseDouble(inches.getText());
            double wrist = Double.parseDouble(circum.getText());
            double num1 = height*height;
            double bmi = (pounds/num1)*703;
     
            //totalBMI.setText("Your more accurate BMI is: " + bmiTot);
            //totalBMI.setText("Your more accurate BMI is: " + );
            double bmiTot = if (gender == () | gender == ()){
                if (height >= 65 && wrist >= 7.5){
                    bmiTot = bmi*.9;
                }else   {
                    bmiTot = bmi;
                }                
                }else   {
                    if (height >= 65 && wrist >= 6.5 || height > 62 && height < 65 && wrist >= 6.25 || height <= 62 && wrist > 5.75){
                        bmiTot = bmi*.9;
                }else   {
                        bmiTot = bmi;
                    }
            };
            totalBMI.setText("Your more accurate BMI is: " + bmiTot);

    double bmiTot = if (gender == () | gender == ()){

    now gives 3 errors stating illegal start of statement

    the "t" and "T" are options for a string entry
    Last edited by XSmoky; November 6th, 2011 at 04:56 PM.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: having a little trouble

    double bmiTot = if (gender == () | gender == ()){

    If statements do not return a value. So what value are your expecting to be assigned to the bmiTot variable?
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: having a little trouble

    bmiTot is supposed to return the result of of the if statement's calculation

    if i enter it like this:

    double bmiTot = //then my error is here (illegal start of expression)
    if (gender.equals(T) | gender.equals(t)){
    if (height >= 65 && wrist >= 7.5){
    bmiTot = bmi*.9;
    }else {
    bmiTot = bmi;
    }
    }else {
    if (height >= 65 && wrist >= 6.5 || height > 62 && height < 65 && wrist >= 6.25 || height <= 62 && wrist > 5.75){
    bmiTot = bmi*.9;
    }else {
    bmiTot = bmi;
    }
    };
    totalBMI.setText("Your more accurate BMI is: " + bmiTot);
    Last edited by XSmoky; November 6th, 2011 at 05:15 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: having a little trouble

    illegal start of expression
    What expression do you have to the right of the = ?
    Give bmiTot an initial value of 0.0

  11. The Following User Says Thank You to Norm For This Useful Post:

    XSmoky (November 6th, 2011)

  12. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: having a little trouble

    nevermind. I figured it out, my space provided wasn't big enough.

    thank you guys so much for helping me with this!!!

    here is the completed code if anyone wants it for anything

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class AccurateBMI extends JComponent implements ActionListener {
        String t;
        String T;
        JFrame frame = new JFrame("A More Accurate BMI Calculator");
        Container content = frame.getContentPane();
        JLabel male = new JLabel();
        JTextField trueFalse = new JTextField();
        JLabel weight = new JLabel();
        JTextField heavy = new JTextField();
        JLabel height = new JLabel();
        JTextField inches = new JTextField();
        JLabel wrist = new JLabel();
        JTextField circum = new JTextField();
        JButton calcBMI = new JButton();
        JLabel totalBMI = new JLabel();
     
        public static void main(String[] args) {
            AccurateBMI application = new AccurateBMI();   
        }
        public void actionPerformed(ActionEvent event) {
            String gender = trueFalse.getText();
            double pounds = Double.parseDouble(heavy.getText());
            double height = Double.parseDouble(inches.getText());
            double wrist = Double.parseDouble(circum.getText());
            double num1 = height*height;
            double bmi = (pounds/num1)*703;
     
            //totalBMI.setText("Your more accurate BMI is: " + bmiTot);
            //totalBMI.setText("Your more accurate BMI is: " + );
            double bmiTot = 0.0;
            if (gender.equals(T) | gender.equals(t)){
                if (height >= 65 && wrist >= 7.5){
                    bmiTot = bmi*.9;
                }else   {
                    bmiTot = bmi;
                }                
                }else   {
                    if (height >= 65 && wrist >= 6.5 || height > 62 && height < 65 && wrist >= 6.25 || height <= 62 && wrist > 5.75){
                        bmiTot = bmi*.9;
                }else   {
                        bmiTot = bmi;
                    }
            };
            totalBMI.setText("Your more accurate BMI is: " + bmiTot);
     
        }
        public AccurateBMI()  {       
            content.setBackground(Color.white);
            content.setLayout(null);
            content.setForeground(Color.yellow);
            content.setFont(new Font("Arial", Font.ITALIC, 14));
            content.add(this);
     
            male.setText("You are a male. T or F: ");
            male.setLocation(5,5);
            male.setSize(300,30);
            content.add(male);
     
            trueFalse.setText("");
            trueFalse.setLocation(310,5);
            trueFalse.setSize(50,30);
            content.add(trueFalse);
            trueFalse.addActionListener(this);
     
            weight.setText("How much do you weigh in pounds?");
            weight.setLocation(5,50);
            weight.setSize(300,30);
            content.add(weight);
     
            heavy.setText("");
            heavy.setLocation(310,50);
            heavy.setSize(50,30);
            content.add(heavy);
            heavy.addActionListener(this);
     
            height.setText("Please enter your height in inches: ");
            height.setLocation(5,95);
            height.setSize(300,30);
            content.add(height);
     
            inches.setText("");
            inches.setLocation(310,95);
            inches.setSize(50,30);
            content.add(inches);
            inches.addActionListener(this);
     
            wrist.setText("How many inches around is your wrist? ie: 7.25 ");
            wrist.setLocation(5,140);
            wrist.setSize(300,30);
            content.add(wrist);
     
            circum.setText("");
            circum.setLocation(310,140);
            circum.setSize(50,30);
            content.add(circum);
            circum.addActionListener(this);
     
            calcBMI.setText("Calculate");
            calcBMI.setLocation(130,180);
            calcBMI.setSize(100,30);
            content.add(calcBMI);
            calcBMI.addActionListener(this);
     
            totalBMI.setText("  ");
            totalBMI.setLocation(100,220);
            totalBMI.setSize(200,30);
            content.add(totalBMI);        
     
            frame.setLocation(0,0);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(385,300);
            frame.setVisible(true);
    }
    }
    Last edited by XSmoky; November 6th, 2011 at 08:53 PM.

Similar Threads

  1. Having trouble with if, else if, else if...
    By thetallguy in forum Loops & Control Statements
    Replies: 1
    Last Post: October 24th, 2011, 10:28 PM
  2. jTable trouble...
    By _lithium_ in forum AWT / Java Swing
    Replies: 3
    Last Post: March 6th, 2011, 07:05 PM
  3. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 04:00 PM
  4. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  5. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM