Results 1 to 2 of 2
  1. #1
    celeste is offline Member
    Join Date
    Apr 2014
    Rep Power

    Default trouble writing if statement with combo box

    This is my first post and I'm very new to programming. I'm working on assignment for a class where I'm creating a home utility auditor. Basically the user has to pick from a list of appliances and then input various fields to calculate a total cost. When the user chooses "washer" from the list of appliances, they must enter in values for 2 separate fields, and will then click calculate to get total cost. I've been trying to implement this through an if, else statement, but it's not wanting to calculate a total cost and I'm running into errors. I've put the code below. Any help would be greatly appreciated. Thank you!

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // The calculate button will multiply the 3 fields that the user inputs:
    //cost*Kw used*hours used=total cost of operation
    float num1,num2, num3, result;
    //now, parse the text to a type float
    num1= Float.parseFloat (costField.getText ());
    num2= Float.parseFloat (kilowattsField.getText ());
    num3= Float.parseFloat (hoursField.getText ());
    result= (float) (num1*num2*num3);
    float num4, num5;
    num4= Float.parseFloat (gallonsField.getText ());
    num5= Float.parseFloat (costgallonsField.getText ());

    if (jComboBox1.getSelectedItem().equals("Washer"))
    result= (float) (num4*num5*num3);
    else {
    result= (float) (num1*num2*num3);


  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: trouble writing if statement with combo box

    If you run into errors, don't you think it would be a good idea to post those errors?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. help writing a statement in java
    By thclement in forum New To Java
    Replies: 1
    Last Post: 01-24-2013, 06:04 AM
  2. Trouble with While Statement
    By Chad.Curtiss in forum New To Java
    Replies: 9
    Last Post: 07-20-2012, 07:03 AM
  3. Trouble with recursion if statement.
    By david522 in forum New To Java
    Replies: 3
    Last Post: 09-28-2011, 02:41 PM
  4. new to java, trouble with if statement
    By Globular in forum New To Java
    Replies: 8
    Last Post: 05-25-2011, 06:03 PM
  5. Combo box trouble turning into a boxing match with java
    By Embercloud in forum New To Java
    Replies: 1
    Last Post: 01-05-2011, 04:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts