# Thread: 2.degree equation. Why String?

1. Member Join Date
Sep 2014
Posts
47
Rep Power
0

## 2.degree equation. Why String?

Hi I just created a program to calculate the roots for a 2. degree equation, I have pasted my code below.
When I first created my program, I defined the r1 and r2 as double, but got an error saying they needed to be defined as String, why is that?
When should I be using string? I am new to java and I have mostly only used int, double and Boolean so far.

Thanks for the help :)

import java.util.Scanner;
import java.text.DecimalFormat;

public class c3e1 {
public static void main(String[]args){

System.out.println("Type in a b and c for 2. degree equation");

Scanner input = new Scanner (System.in);

double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();

DecimalFormat df = new DecimalFormat("0.######");

String r1 = df.format((-b + Math.pow((b*b)-(4*a*c),0.5)) / (2*a));
String r2 = df.format((-b - Math.pow((b*b)-(4*a*c),0.5)) / (2*a));

System.out.println("The roots are" + r1 + " and " + r2);

}
}  Reply With Quote

2. ## Re: 2.degree equation. Why String?

Read the API documentation for the format( ... ) method: it takes a double as a parameter and returns a String typed result; if you want to assign its result to r1 or r2, then r1 and r2 have to be of type String.

kind regards,

Jos  Reply With Quote

3. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: 2.degree equation. Why String?

You could also just assign the computed results to a double and not use DecimalFormat. You could then use System.out.printf()
to format the output (similar to C if you are familiar with that).

Regards,
Jim  Reply With Quote

4. Member Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: 2.degree equation. Why String?

josAH, I dont really understand your answer, would it be possible to simply it a little for a beginner?

Jim829: Thanks, So I can keep it a double (instead of string), and use the System.out.printf() when I print ? How do I set the format I want, to for instance two decimals and print both r1 and r2 with two decimals.

I appreciate all the great help! Makes it alot easier and more enjoyable to learn java  Reply With Quote

5. ## Re: 2.degree equation. Why String?  Reply With Quote

6. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: 2.degree equation. Why String? Originally Posted by Thammer josAH, I dont really understand your answer, would it be possible to simply it a little for a beginner?
He explained it. It returns a formatted string (using the computed value and the specified format).
Jim829: Thanks, So I can keep it a double (instead of string), and use the System.out.printf() when I print ? How do I set the format I want, to for instance two decimals and print both r1 and r2 with two decimals.
Read the documentation on the Formatter class in the JDK. System.out.printf
(which is a method of the PrintStream class) uses it.

Regards,
Jim  Reply With Quote

7. Member Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: 2.degree equation. Why String?  Reply With Quote