Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:17:42+00:00 2026-05-28T19:17:42+00:00

My Java progpram won’t process -1 in a string correctly. It skips through all

  • 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;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:17:43+00:00Added an answer on May 28, 2026 at 7:17 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program that loads thirdparty class files (classes I did not
We have a java program that requires a large amount of heap space -
We have a Java program run as root on Unix, that therefore can read
I am writing this java program to find all the prime numbers up to
I'm using JNI to access a native C file through my java program. I've
I'm using JSON with Gson package for java. I have an object where there
I have a Java program which uses a lot of text files. The program
My Ant script won't compile code. It says it doesn't find a C:\Program Files\Java\jdk1.6.0_25.
I'm trying to make an array of integers in java but it won't work
I have REST Web service written in Java. Now I want to disable Web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.