0

I'm trying to create a simple ideal weight calculator. It accepts two types of inputs, apart from your name - metric and imperial inputs, specifically kg and pounds respectively. In order to designate whether the following field specifies kg or pounds, I have a text input for metric or imperial, where by typing m/M or i/I you pick between metric and imperial.

It looks like this.

Name: ________
(M)etric or (I)mperial: ________
Weight: _________
      [[Calculate]]

Here is what the code is:

private void btnInputActionPerformed(java.awt.event.ActionEvent evt) {

//Acquire name string info
    String name = this.txtInput_name.getText();

//Aquire system of measurement info
    String SOM = this.txtInput_som.getText();

//Acquire height information
    String heightInput = this.txtInput_height.getText();
    double height = Double.parseDouble(heightInput);

    //Set up if statement to deal with I, M, or neither, if user inputs something else
    //For imperial (designation i, I):
    if ((SOM == "I") || (SOM == "i")) {
    //Imperial: Weight (pounds) = height (inches) × height (inches) × 25 ÷ 703    
        double weightlb = Math.pow(height, 2)*25/703;
        this.lblOutput.setText(name + "'s ideal weight is " + weightlb + " pounds.");
    }

    //For metric (designation m, M):
    else if ((SOM == "M") || (SOM == "m")){
    //Metric:Weight (kg) = height (metres) × height (metres) × 25
        double weightkg = Math.pow(height, 2)*25;
        this.lblOutput.setText(name + "'s ideal weight is " + weightkg + "kg.");
    }

    //For user not inputting any taken designation for metric or imperial as above (e.g. "Celsius", "g", "turkey", etc.)
    else {
        //Not going to tell them about other ways of designating metric/imperial, label text too long.
        this.lblOutput.setText("Please input a valid system of measurement, designation: I or M.");
    }


}  

The issue is, despite typing in M/m/i/I (one of those at a time!) for Metric or Imperial, it always spits out the result of the else statement - Please input a valid system of measurement...

Have I done something wrong with the conditions of the if/else if statement, or is it something else altogether?

RtMa
  • 15
  • 3
  • 1
    Don't compare Strings using `==`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *objects* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So not `if ((SOM == "I") || (SOM == "i")) {` but instead `if (("I".equalsIgnoreCase(SOM)) {` – Hovercraft Full Of Eels Jul 11 '14 at 01:06
  • Next, you will want to learn and follow Java naming conventions so that others can more easily understand your code. Variables and method names should all begin with a lower-case letter while class names should begin with an upper-case letter. All-caps should be reserved for constants. – Hovercraft Full Of Eels Jul 11 '14 at 01:07

0 Answers0