Diestro
Diestro

Reputation: 11

Format to show positive or negative sign of a number in java

Good day! I made a program that would find an equation of a locus of point and I want to have my output to show a "+" sign if it is a positive number. Yet, it shows a "-" sign when its negative because I reciprocate their values. Here is my code:

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

        int x1 , y1, x2, y2,
            inverted_x1, inverted_y1, inverted_x2, inverted_y2,
            x1Times2, x2Times2, y1Times2, y2Times2,
            x1raised, y1raised, x2raised, y2raised,
            computeX, computeY, computeS,
            x2inverted, y2inverted, invertedx2times2, invertedy2times2;

        try{
            x1 = Integer.parseInt(jTextField1.getText());
            y1 = Integer.parseInt(jTextField2.getText());
            x2 = Integer.parseInt(jTextField3.getText());
            y2 = Integer.parseInt(jTextField4.getText());

            int[] array = new int[4];
                  array[0] = x1;
                  array[1] = x2;
                  array[2] = y1;
                  array[3] = y2;

            inverted_x1 = x1 *= -1;
            inverted_y1 = y1 *= -1;
            inverted_x2 = x2 *= -1;      
            inverted_y2 = y2 *= -1;          

            jTextField5.setText("(x" + Integer.toString(inverted_x1) + ")² + (y" + Integer.toString(inverted_y1) + ")²" 
                    + " = (x" + Integer.toString(inverted_x2) + ")² + (y" + Integer.toString(inverted_y2) + ")²" );

            x1Times2 = inverted_x1*2;
            y1Times2 = inverted_y1*2;           
            x2Times2 = inverted_x2*2;
            y2Times2 = inverted_y2*2;                                   
            x1raised = inverted_x1*inverted_x1;
            y1raised = inverted_y1*inverted_y1;         
            x2raised = inverted_x2*inverted_x2;
            y2raised = inverted_y2*inverted_y2;

            jTextField9.setText("x" + Integer.toString(x1Times2) + "x" + Integer.toString(x1raised) + "+y²" + Integer.toString(y1Times2)
                    + "y" + Integer.toString(y1raised) + "= x²" + Integer.toString(x2Times2) + "x" + Integer.toString(x2raised) + "+y²"
                    + Integer.toString(y2Times2) + "y" + Integer.toString(y2raised));            

            x2inverted = x2raised *= -1;
            y2inverted = y2raised *= -1;
            invertedx2times2 = x2Times2 *= -1;
            invertedy2times2 = y2Times2 *= -1;
            computeX = x1Times2 + invertedx2times2;
            computeY = y1Times2 + invertedy2times2;
            computeS = x2inverted + y2inverted + x1raised + y1raised;

            jTextField17.setText(Integer.toString(computeX) + "x" + Integer.toString(computeY) + "y" + Integer.toString(computeS) + "=0");                                                                                 

            XYLineChart_AWT chart = new XYLineChart_AWT("Locus of Point Graph", "", array);
            chart.pack( );          
            RefineryUtilities.centerFrameOnScreen( chart );          
            chart.setVisible( true );             

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Please fill necessary inputs");
        }
    }      

Here is a sample output: Click Me. Can I have my output to show "+" sign if its positive?

Thank you in advance!

Upvotes: 1

Views: 1840

Answers (1)

showp1984
showp1984

Reputation: 378

You can check the number with a conditional expression and then print it as String with the sign:

private static String formatSign(int number) {
    return (number > 0 ? "+" : "" ) + number;
}

Since you didn't clarify how to handle the 0, I assumed you wanted to leave it unsigned.

Output example:

-5
0
+5

Upvotes: 1

Related Questions