Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 1st, 2012, 02:35 AM
 tryingtolearn
Registered User

Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 56 m 21 sec
Reputation Power: 0
Need Help With Number Alignment

Hello everyone!

I am in the process of creating a program that finds the fourth variable value (a,b,c, or x) within the quadratic formula. I am trying to display my results in combination with what I assign to the other three values. For example, I would want to say:

Given that:
CoefficientOfXSquared = 4.0 (these numbers are for e.x.)
CoefficientOfX = 2.0
TheConstant = 6.0
TheRoot = -1.7777766 (this last value is found by plugging in the other three into the equation).

Basically, I am trying to right align the numbers on the right side of the equal sign, so it would look something like this:

= 4.0
= 2.0
= 6.0
= -1.7777766

i initially thought I could just put some spaces or '\t's in there, but I realized I need to craft in in such a way that the values can be changed and the numbers still be flush with the right side.

Can anybody guide me towards the right direction?

Thanks so much!
tryingtolearn
Code:
```public class JQuadratic
{
public static void main(String[] Theory)
{
double CoefficientOfXSquared, CoefficientOfX, TheConstant, TheRoot;

// PROBLEM #1 - SOLVING FOR 'A' VALUE

System.out.println("\t\t\t\t   Problem  #1\n");

CoefficientOfX        =  5;
TheConstant           =  4;
TheRoot               =  6;
CoefficientOfXSquared = (-CoefficientOfX * TheRoot - TheConstant)/(TheRoot * TheRoot);

System.out.println( "\t\t\t\t   Given that:"                                                );
System.out.println("\t\t        CoefficientOfX        = " + CoefficientOfX         + " and ");
System.out.println("\t\t        TheConstant           = " + TheConstant            + " and ");
System.out.println("\t\t        TheRoot               = " + TheRoot                + " then");
System.out.println("\t\t        CoefficientOfXSquared = " + CoefficientOfXSquared  + " \n\n");

// PROBLEM #2 - SOLVING FOR 'B' VALUE

System.out.println("\n\n\t\t\t\t   Problem  #2\n");

CoefficientOfXSquared = 5;
TheConstant           = 2;
TheRoot               = 3;
CoefficientOfX        = (-TheConstant - CoefficientOfXSquared * Math.pow(TheRoot,2))/TheRoot;

System.out.println( "\t\t\t\t   Given that:"                                               );
System.out.println("\t\t        CoefficientOfXSquared = " + CoefficientOfXSquared + " and ");
System.out.println("\t\t        TheConstant           = " + TheConstant           + " and ");
System.out.println("\t\t        TheRoot               = " + TheRoot               + " then");
System.out.println("\t\t        CoefficientOfX        = " + CoefficientOfX        + "\n\n" );

// PROBLEM #3 - SOLVING FOR 'C' VALUE

System.out.println("\n\n\t\t\t\t   Problem  #3\n");

CoefficientOfXSquared = 67 ;
CoefficientOfX        = 78 ;
TheRoot               = 100;
TheConstant           = -CoefficientOfXSquared * Math.pow(TheRoot, 2) - CoefficientOfX * TheRoot;

System.out.println( "\t\t\t\t   Given that:"                                               );
System.out.println("\t\t        CoefficientOfXSquared = " + CoefficientOfXSquared + " and ");
System.out.println("\t\t        CoefficientOfX        = " + CoefficientOfX        + " and ");
System.out.println("\t\t        TheRoot               = " + TheRoot               + " then");
System.out.println("\t\t        TheConstant           = " + TheConstant           + " \n\n");

// PROBLEM #4 - SOLVING FOR 'X' VALUE

System.out.println("\n\n\t\t\t\t   Problem  #4\n");

CoefficientOfXSquared = 3;
CoefficientOfX        = 9;
TheConstant           = 6;
TheRoot               = (-CoefficientOfX + Math.sqrt(Math.pow(CoefficientOfX,2) - 4 * CoefficientOfXSquared * TheConstant)) / (2 * CoefficientOfXSquared);

System.out.println( "\t\t\t\t   Given that:"                                               );
System.out.println("\t\t        CoefficientOfXSquared = " + CoefficientOfXSquared + " and ");
System.out.println("\t\t        CoefficientOfX        = " + CoefficientOfX        + " and ");
System.out.println("\t\t        TheConstant           = " + TheConstant           + " then");
System.out.println("\t\t        TheRoot               = " + TheRoot               + " \n\n");

System.out.println("\n\n\n");
}
}```

#2
July 1st, 2012, 07:32 AM
 leo2012
Contributing User

Join Date: Jun 2012
Posts: 50
Time spent in forums: 1 Day 17 h 24 m 13 sec
Reputation Power: 2
Use print format

ex:

Code:
```int x = 2;
System.out.format("%5d", x);```

another way to do it using java.util.Formatter

Code:
```import java.util.Formatter;

public class Formatting {

public static void main(String[] args) {

double data[] = { 12.5, 30.6, 8.25, 2.03, 5.3};

Formatter fmt = new Formatter();

fmt.format("%10s %20s\n", "Value", "Square Root");

for (double d : data) {
fmt.format("%10.3f %20.3f\n", d, Math.sqrt(d));
}
System.out.println(fmt);

}```

#3
July 1st, 2012, 08:38 AM
 tryingtolearn
Registered User

Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 56 m 21 sec
Reputation Power: 0
Quote:
 Originally Posted by leo2012 Use print format ex: Code: ```int x = 2; System.out.format("%5d", x);``` another way to do it using java.util.Formatter Code: ```import java.util.Formatter; public class Formatting { public static void main(String[] args) { double data[] = { 12.5, 30.6, 8.25, 2.03, 5.3}; Formatter fmt = new Formatter(); fmt.format("%10s %20s\n", "Value", "Square Root"); for (double d : data) { fmt.format("%10.3f %20.3f\n", d, Math.sqrt(d)); } System.out.println(fmt); }```

When I try it using the first method, I will put something like:

Code:
`System.out.printf("%-23d%2.3%n", "TheCoefficientOfXSquared is ", TheCoefficicientOfXSquared);`

But it is all wacky. I am trying to make it so there are digits in the ones and tens place and three digits in the tenths, hundredths, and thousandths place (three decimals spaces).

#4
July 1st, 2012, 09:30 AM
 leo2012
Contributing User

Join Date: Jun 2012
Posts: 50
Time spent in forums: 1 Day 17 h 24 m 13 sec
Reputation Power: 2
Quote:
 Originally Posted by tryingtolearn When I try it using the first method, I will put something like: Code: `System.out.printf("%-23d%2.3%n", "TheCoefficientOfXSquared is ", TheCoefficicientOfXSquared);` But it is all wacky. I am trying to make it so there are digits in the ones and tens place and three digits in the tenths, hundredths, and thousandths place (three decimals spaces).

restudy the example and and play around changing values! (understand what is for s , f, d (\n = new line) )

Code:
`System.out.printf("%10s %2.3f", "TheCoefficientOfXSquared is ", CoefficientOfXSquared);`

docs.oracle.com/javase/tutorial/java/data/numberformat.html

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Need Help With Number Alignment