Jump to content

help with if-statement

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
I'm taking a Java class, and each week we have a homework assignment. Then each homework assignment gets added to our 'final project'.

We've got a jFrame with a 6 buttons at the top, one for each homework assignment. Each button uses the same event, the event just checks to see which text is on the button to determine what to do.

Now, for example, when the user clicks HW3 it'll display a message telling them some instructions. Then they click the HW3 button again & it'll take the values and do some calculations.

I can get that to work fine, but I also need a message that tells the user to enter values if they leave them empty. I can't get this to work because the values are empty to start with so it bypasses my 'instructions' message and skips straight to the message telling them that the fields are empty.

I'm not sure how to get this working right. Any ideas? The code is below.

Thanks!


            if ("HW3".equals(actionCommand)) {

                msg = "Please enter the required data in the fields to the left. Then click the 'HW3' button again.\n"

                    + "\n'P' = Price per gallon."

                    + "\n'M' = Miles per gallong."

                    + "\n'D' = Distance traveled."; 


                jTextField1.setVisible(true);

                jTextField2.setVisible(true);

                jTextField3.setVisible(true);

                jLabel1.setText("P:");

                jLabel2.setText("M:");

                jLabel3.setText("D:");


                double gasPrice, miGal, distance;

                double tankSize = 16;

                String gPStr, mGStr, diStr;


                gPStr = jTextField1.getText();

                mGStr = jTextField2.getText();

                diStr = jTextField3.getText();


                if(gPStr.trim().length() != 0 && mGStr.trim().length() != 0 && diStr.trim().length() != 0){

                    gasPrice = Double.parseDouble(gPStr);

                    miGal = Double.parseDouble(mGStr);

                    distance = Double.parseDouble(diStr);


                    Hw3Car myCar = new Hw3Car(miGal);

                    myCar.setFuelLevel(tankSize);

                    myCar.drive(distance);

                    msg = (myCar.getFuelLevel() < 0.1)? // getter / shorthand If statement - if FuelLevel is less than 0.1

			"You ran out of gas." : // you ran out of gas

			"The amount of gas left in the tank is " + myCar.getFuelLevel() + "," // else builds the string displays your info

			+ "\n" + " and you got " + myCar.truEff(gasPrice) + " miles per dollar.";

                } else {

                    if(gPStr.trim().length() == 0 || mGStr.trim().length() == 0 || diStr.trim().length() == 0){

                    msg = "Please enter values in all fields!";

                    }

                }

            }


Posted Image

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
The easiest thing i can think of is making an integer: status
status will have values from 0 up to and including 6. When the aplication starts it's 0.

When you press a button it will receive the number of the assignment. So for the homework3 button, "status" will be 3.

Your code for the button will look like:

if ("HW3".equals(actionCommand)) {

                msg = "Please enter the required data in the fields to the left. Then click the 'HW3' button again.\n"

                    + "\n'P' = Price per gallon."

                    + "\n'M' = Miles per gallong."

                    + "\n'D' = Distance traveled."; 


                jTextField1.setVisible(true);

                jTextField2.setVisible(true);

                jTextField3.setVisible(true);

                jLabel1.setText("P:");

                jLabel2.setText("M:");

                jLabel3.setText("D:");

               

             [COLOR="lime"][B]   if(status == 3){ [/B][/COLOR]

                  [COLOR="lime"][B]//you can check the textfields here, as they now should be filled in correctly.[/B][/COLOR]

                  double gasPrice, miGal, distance;

                  double tankSize = 16;

                  String gPStr, mGStr, diStr;


                  gPStr = jTextField1.getText();

                  mGStr = jTextField2.getText();

                  diStr = jTextField3.getText();


                  if(gPStr.trim().length() != 0 && mGStr.trim().length() != 0 && diStr.trim().length() != 0){

                      gasPrice = Double.parseDouble(gPStr);

                      miGal = Double.parseDouble(mGStr);

                      distance = Double.parseDouble(diStr);


                      Hw3Car myCar = new Hw3Car(miGal);

                      myCar.setFuelLevel(tankSize);

                      myCar.drive(distance);

                      msg = (myCar.getFuelLevel() < 0.1)? // getter / shorthand If statement - if FuelLevel is less than 0.1

	    		  "You ran out of gas." : // you ran out of gas

			  "The amount of gas left in the tank is " + myCar.getFuelLevel() + "," // else builds the string displays your info

			  + "\n" + " and you got " + myCar.truEff(gasPrice) + " miles per dollar.";

                  } else {

                      if(gPStr.trim().length() == 0 || mGStr.trim().length() == 0 || diStr.trim().length() == 0){

                      msg = "Please enter values in all fields!";

                      }

                  }

             [B][COLOR="lime"] } else {

                 status = 3;

              }[/COLOR][/B]

            }



#3
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
That makes sense..

It's really simple, but I wouldn't have thought of it because the teacher said we'd just be copying/pasting in our homeworks & just changing a few things (homeworks use joptionpanes * loops that wont be needed in the project).


Thanks!
Posted Image