Results 1 to 7 of 7
  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default 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);

    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: 2.degree equation. Why String?

    Thanks for the answers!


    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

  5. #5
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: 2.degree equation. Why String?


  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: 2.degree equation. Why String?

    Quote Originally Posted by Thammer View Post
    Thanks for the answers!
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: 2.degree equation. Why String?

    Thanks for the answers!
    Very helpful! :)

Similar Threads

  1. Is a B.S. degree necessary for a software engineer?
    By eviljeff in forum Jobs Discussion
    Replies: 2
    Last Post: 07-26-2013, 09:09 AM
  2. Input String to boolean equation
    By rru96 in forum New To Java
    Replies: 10
    Last Post: 09-23-2012, 10:21 AM
  3. a bachelor's degree OR a SCJP certification??
    By kulangotski in forum Jobs Discussion
    Replies: 5
    Last Post: 09-18-2011, 03:25 AM
  4. Choosing a degree concentration
    By xcallmejudasx in forum Jobs Discussion
    Replies: 11
    Last Post: 08-14-2009, 11:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •