Hi All
Kindly assist am having issue with the following code, where did i get it wrong? Only the first Block condition is execute, even if B<PA, the second block does not execute. Am using Netbean

        PA =((1.1 * P2) / (1.46 * P3));

        B = 3 * P4;
        D = P4;

        B1 = PA + 0.02;
        D1 = ((B - P4) / 2);

        if (B > PA) {
            txtB.setText("" + Round(B,2));
            txtD.setText("" + Round(D,2));
            patxt.setText(""+ PA);
        }
        else {
            txtB.setText("" + Round(B1,2));
            txtD.setText("" + Round(D1,2));
            patxt.setText(""+ PA);
        }

Recommended Answers

All 5 Replies

I don't see anywhere that you have set vales for P2, P3 or P4 so don't see how you expect this to work

It's highly unlikely that you have found a bug in Java's execution of if tests, so try a little debugging to confirm what's happening. Print B and PA immediately before the if test, put one print statement in the if, one in the else to confirm what is or isn't being executed.

Am using netbeans, so P2, P3 or P4 are actual the inputs(double variable): I actually just cut of the Conditional Statement :
This the lenthy Code: though still reduced:
String m = pcTxt.getText();

    P2 = Double.parseDouble(n);
    String o = pbTxt.getText();
    P3 = Double.parseDouble(o);
    String p = tTxt.getText();
    P4 = Double.parseDouble(p);


    PA =((1.1 * P2) / (1.46 * P3));
    B = 3 * P4;
    D = P4;

    B1 = PA + 0.02;
    D1 = ((B - P4) / 2);

    if (B > PA) {
        txtB.setText("" + Round(B,2));
        txtD.setText("" + Round(D,2));
        //patxt.setText(""+ PA);
    }
    else {
        txtB.setText("" + Round(B1,2));
        txtD.setText("" + Round(D1,2));
        //patxt.setText(""+ PA);
    }

the output is to be displayed in txtB and txtD which are jTextFields, the first condition works just fine, but d second doesn't

... try a little debugging to confirm what's happening. Print B and PA immediately before the if test, put one print statement in the if, one in the else to confirm what is or isn't being executed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.