0

My Java progpram won't process -1 in a string correctly. It skips through all those if statements I have in the e.getActionCommand() for "equation". I tried making a character array from the string and processing character[0] and that didn't work either.

    if ("equation".equals(e.getActionCommand()))
    {
        if (interceptB != "0" && slopeX == "1")
        {
            if (strIntercept != "")
            {
                String slash = "[/]";
                String[] slashedUp = interceptB.split(slash);
                float numer = new Float(slashedUp[0]);
                float  denomer = new Float(slashedUp[1]);
                float fl = numer / denomer;
                String minus = "[-]";
                String[] splitUp = interceptB.split(minus);
                if (fl > 0)
                {
                    textEquation.setText("y = x + " + interceptB);
                }
                else if (fl < 0)
                {
                    textEquation.setText("y = x - " + splitUp[1]);
                }
            }

            else
            {
                float numb = new Float(interceptB);
                String minus = "[-]";
                String[] splitUp = interceptB.split(minus);
                if (numb > 0)
                {
                    textEquation.setText("y = x + " + interceptB);
                }
                else if (numb < 0)
                {
                    textEquation.setText("y = x - " + splitUp[1]);
                }
            }
        }
        else if (interceptB != "0" && slopeX == "-1")
        {
            if (strIntercept != "")
            {
                String slash = "[/]";
                String[] slashedUp = interceptB.split(slash);
                float numer = new Float(slashedUp[0]);
                float  denomer = new Float(slashedUp[1]);
                float fl = numer / denomer;
                String minus = "[-]";
                String[] splitUp = interceptB.split(minus);
                if (fl > 0)
                {
                    textEquation.setText("y = -x + " + interceptB);
                }
                else if (fl < 0)
                {
                    textEquation.setText("y = -x - " + splitUp[1]);
                }
            }

            else
            {
                float numb = new Float(interceptB);
                String minus = "[-]";
                String[] splitUp = interceptB.split(minus);
                if (numb > 0)
                {
                    textEquation.setText("y = -x + " + interceptB);
                }
                else if (numb < 0)
                {
                    textEquation.setText("y = -x - " + splitUp[1]);
                }
            }
        }
        else if (slopeX != "0" && interceptB == "0")
        {
            if (slopeX == "1")
            {
                textEquation.setText("y = x");
            }
            else if (slopeX == "-1")
            {
                textEquation.setText("y = -x");
            }
            else
            {
                textEquation.setText("y = " + slopeX + "x");
            }
        }
        else if (slopeX == "0" && interceptB != "0")
        {
            textEquation.setText("y = " + interceptB);
        }
        else
        {
            if (strIntercept != "")
            {
                String slash = "[/]";
                String[] slashedUp = interceptB.split(slash);
                float numer = new Float(slashedUp[0]);
                float  denomer = new Float(slashedUp[1]);
                float fl = numer / denomer;
                String minus = "[-]";
                String[] splitUp = interceptB.split(minus);
                if (fl > 0)
                {
                    textEquation.setText("y = " + slopeX + "x" + " + " + interceptB);
                }
                else if (fl < 0)
                {
                    textEquation.setText("y = " + slopeX + "x" + " - " + splitUp[1]);
                }
            }

            else
            {
                float numb = new Float(interceptB);
                String minus = "[-]";
                String[] splitUp = interceptB.split(minus);
                if (numb > 0)
                {
                    textEquation.setText("y = " + slopeX + "x" + " + " + interceptB);
                }
                else if (numb < 0)
                {
                    textEquation.setText("y = " + slopeX + "x" + " - " + splitUp[1]);
                }
            }
        }
    }
    if ("reset".equals(e.getActionCommand()))
    {
        point2.setEnabled(false);
        getSlope.setEnabled(false);
        getIntercept.setEnabled(false);
        totalEquation.setEnabled(false);
        firstPoint = null; 
        secondPoint = null; 
        slope = null;
        totalSlope = null;
        strIntercept = null; 
        slopeX = null; 
        interceptB = null;
        x1 = 0; 
        x2 = 0; 
        y1 = 0;
        y2 = 0; 
        slopex = 0; 
        slopey = 0; 
        simplifiedSlope = 0; 
        mx = 0; 
        totalMx = 0; 
        intercept = 0; 
        ySide = 0; 
        totalY = 0;
        textField1.setText("");
        textField2.setText("");
        textSlope.setText("");
        textIntercept.setText("");
        textEquation.setText("");
    }
}

public void GetFirstPoint()
{
    firstPoint = textField1.getText();
    String comma = "[,]";
    String[] x1Y1 = firstPoint.split(comma);
    x1 = new Float(x1Y1[0]);
    y1 = new Float(x1Y1[1]);
}

public void GetSecondPoint()
{
    secondPoint = textField2.getText();
    String comma = "[,]";
    String[] x2y2 = secondPoint.split(comma);
    x2 = new Float(x2y2[0]);
    y2 = new Float(x2y2[1]);
}   

public void GetSlope()
{
    slopey = y2 - y1;
    slopex = x2 - x1;

    if (slopey < 0 && slopex < 0)
    {
        slopey = -slopey;
        slopex = -slopex;
    }
    if (slopex < 0)
    {
        slopey = -slopey;
        slopex = -slopex;
    }
    int slopeGCF = calcGCF(slopey, slopex);

    if (slopey % slopex == 0)
    {
        simplifiedSlope = (int)slopey/(int)slopex;
        totalSlope = "";
    }
    else if (slopey % slopex != 0 && slopeGCF > 0)
    {
        slopey = slopey / slopeGCF;
        slopex = slopex / slopeGCF;
        String slopeY = RoundUp(slopey);
        String slopeX = RoundUp(slopex);
        totalSlope = slopeY + "/" + slopeX;
    }
    else
    {
        String slopeY = RoundUp(slopey);
        String slopeX = RoundUp(slopex);
        totalSlope = slopeY + "/" + slopeX;
    }
}

public void GetIntercept()
{
    if (totalSlope == "")
    {
        mx = simplifiedSlope * x1;
        intercept = y1 - mx;
        strIntercept = "";
    }

    else if (totalSlope != "")
    {
        mx = slopey * x1;

        if (mx % slopex == 0)
        {
            totalMx = mx / slopex;
            intercept = y1 - totalMx;
            strIntercept = "";
        }
        else
        {
            ySide = y1 * slopex;
            totalY = ySide - mx;
            float interceptGCF = calcGCF(totalY, slopex);
            float ceptGCFx = 0;
            float ceptGCFy = 0;
            if (totalY % slopex == 0)
            {
                intercept = (int)totalY / (int)slopex;
                strIntercept = "";
            }

            else if (totalY % slopex != 0 && interceptGCF > 0)
            {
                ceptGCFy = totalY / interceptGCF;
                ceptGCFx = slopex / interceptGCF;
                strIntercept = RoundUp(ceptGCFy) + "/" + RoundUp(ceptGCFx);
            }
            else
            {
                strIntercept = RoundUp(totalY) + "/" + RoundUp(slopex);
            }
        }
    }
}

public int calcGCF(float num, float denom)
{   
    float s;

    if (num < 0)
    {
        num = -num;
    }

    else if (denom < 0)
    {
        denom = -denom;
    }

    if (num > denom)
    {
        s = denom;
    }

    else
    {
        s = num;
    }

    if (s > 0)
    {
        for (float i = s; i > 0; i--)
        {
            if ((num % i == 0) && (denom % i == 0))
            {
                return (int)i;
            }
        }
    }
    return -1;
}

public String RoundUp(float f)
{
    int i = (int)f;
    String valToStore = (i==f) ? String.valueOf(i) : String.valueOf(f);
    return valToStore;
}
}
4
  • 2
    Can you trim the code down to just the relevant part? That is a ton of code. Jan 22, 2012 at 23:51
  • 1
    With James' request, I suggest you create a SSCCE.
    – Jeffrey
    Jan 22, 2012 at 23:51
  • trimmed it this is in the actionPerformed module
    – dsta
    Jan 22, 2012 at 23:52
  • 1
    @SeanSasaki "Trimming" doesn't mean "only all the code I care about", it means "the smallest unit of code that demonstrates the problem". In this case, any command/value recognition, since it all has the same problem. All that code is essentially identical from a logical/flow standpoint. Jan 22, 2012 at 23:59

2 Answers 2

3

String comparisons should use "foo".equals(s), not ==.

(You have a lot of refactoring possibilities; it'd make the code much easier to reason about.)

5
  • sorry i'm really new to Java but what does "foo" mean?
    – dsta
    Jan 22, 2012 at 23:58
  • @SeanSasaki It's a string. It doesn't mean anything. Things in quotes are strings. Jan 23, 2012 at 0:00
  • It's just an example string, the syntax is someString.equals(anotherString). Jan 23, 2012 at 0:00
  • @SeanSasaki Foo is a metasyntactic variable. It's used as a placeholder for some arbitrary value.
    – Jeffrey
    Jan 23, 2012 at 0:01
  • @Sean Foo, bar, baz or foobar are just place holder variables for short code examples where you can't use any sensible names. Quite a long and interesting history it has.
    – Voo
    Jan 23, 2012 at 0:31
1

Use String.equals() instead of == to compare strings. If all strings are known at compile time, or interned, comparison with ==may succeeed, but in general it doesn't, because == compares the object references and not the contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.