1

I am working on a Java project. It is a simple Quadratic Formula code. But when I wanted to convert my double value to a string (where a=1.0, b=0 and c= -4.0 in ax^2+bx+c). My test failed. I tried to figure out what went wrong. I found out where it was and apparently Double.toString(x) where double x=2.0, did not return "2.0". So then I triedString.valueOf(x), which did not work either. I then simple put"2.0"` and it passed my test cases.

Where did I go wrong? Or does Java just hate me.

edit: Wow you guys, commented so quickly. Any advice will be helpful, including things not related to the original topic (repeated syntax or whatever).

Note: As my story says, the only thing I changed was Double.toString(x1) to "2.0" when content was "1.0 0 -4.0" and it passed my test case.

public class QuadraticFormula {
    public String quadraticFormula(String content) { 
        //**
          * content should be a string in the format of "a b c" or this will 
          * return an empty string.
          * returning "" will indicate that content was not correctly inputted.
          * returns a string in the format of "x1 and x2" where x1 and x2 are the 
          * answers from the Quadratic Formula.
          */
        try {
            int m=content.indexOf(" ");
            int n=content.indexOf(" ", m+1);
            if (m==-1||n==-1) {
                return "";
            }
            double a=Double.parseDouble(content.substring(0,m));
            double b=Double.parseDouble(content.substring(m+1,n));
            double c=Double.parseDouble(content.substring(n+1));
            if (b*b<4*a*c) {
                return "imaginary";
            } else {
                double x1=(-b+Math.sqrt(Math.abs(b*b-4*a*c)))/(2*a);
                double x2=(-b-Math.sqrt(Math.abs(b*b-4*a*c)))/(2*a);
                return Double.toString(x1) + " and " + Double.toString(x2); 
            }
        } catch (IndexOutOfBoundsException e) {
            return "";
        } catch (NullPointerException e) {
            return "";
        } catch (NumberFormatException e) {
            return "";
        }       
    }
}
Emissary
  • 9,954
  • 8
  • 54
  • 65
Brad Louie
  • 11
  • 2
  • 2
    can you show us the code you are running ? – Suresh Atta Aug 21 '14 at 09:48
  • What result do you get? – Thirler Aug 21 '14 at 09:48
  • 4
    Welcome to Stackoverflow. Please provide the code you use, since a simple `System.out.println(Double.toString(2.0))` results in 2.0. My best guess is that you use `==` to compare Strings, instead of ``equals() – SirRichie Aug 21 '14 at 09:49
  • 2
    You shouldn't be testing arithmetic code by comparing strings. Compare numbers. – Bohemian Aug 21 '14 at 09:51
  • If you want to format your double result, take a look at http://stackoverflow.com/questions/8819842/best-way-to-format-a-double-value-to-2-decimal-places or http://stackoverflow.com/questions/4885254/string-format-to-format-double-in-java – Aerus Aug 21 '14 at 09:52
  • I would expect it to be "2", but I agree with @Bohemian that what it comes to as a String is irrelevant in a numerical computation. – user207421 Aug 21 '14 at 09:55
  • A note to Shail016: you missed a backtick in your edit, which makes things harder to read now. Sadly, adding the missing backtick is too little an edit to be apprived – SirRichie Aug 21 '14 at 09:56
  • Next thing is that double-precision may interfere. When writing unit-tests on doubles you are normally best-of if you specify how far off the value may be like `Math.abs(got - expected) < 0.00001`. – TheConstructor Aug 21 '14 at 09:58

1 Answers1

2

Simply use following code to convert your double value to string:

String doubleString=new DecimalFormat("#0.00").format(doubleValue);

DecimalFormat comes from java.text package.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34