0
private static double convertCentimeterToInches(double d) {
    double feetPart = 0;
    double inchesPart = 0;
    if (String.valueOf(d) != null && !String.valueOf(d).trim().isEmpty()) {
        feetPart = Math.floor(convertCentimeterToInches(d) / 12);
        inchesPart = Math.ceil(convertCentimeterToInches(d) - (feetPart * 12));
    }
    return inchesPart;
}

06 - 01 02: 25: 08.037: E / XXX(1099): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java: 352) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.util.concurrent.FutureTask.setException(FutureTask.java: 219) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.util.concurrent.FutureTask.run(FutureTask.java: 239) 
06 - 01 02: 25: 08.037: E / XXX(1099): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java: 230)
06 - 01 02: 25: 08.037: E / XXX(1099): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1080) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 573) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.lang.Thread.run(Thread.java: 856) 
06 - 01 02: 25: 08.037: E / XXX(1099): Caused by: java.lang.StackOverflowError 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.lang.RealToString.longDigitGenerator(RealToString.java: 269) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.lang.RealToString.convertDouble(RealToString.java: 111) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.lang.RealToString.doubleToString(RealToString.java: 59) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.lang.Double.toString(Double.java: 317) 
06 - 01 02: 25: 08.037: E / XXX(1099): at java.lang.String.valueOf(String.java: 1625)

I am just thinking on why its throwing stackoverflow error, when everything seems to be perfect in double. There is no conversion happening from double to int or float which could result in stackoverflow.

FDinoff
  • 30,689
  • 5
  • 75
  • 96
theJava
  • 14,620
  • 45
  • 131
  • 172
  • becouse you have unclear condition for exiting from recursing – msangel Jun 01 '13 at 02:33
  • A stack overflow is not caused by any casting between `double`, `int`, `float`, or any other type. A stack overflow is caused almost exclusively by infinite recursion. – Kevin Jun 01 '13 at 02:39

4 Answers4

4

You have an infinite loop, you keep on calling:

convertCentimeterToInches(d)

with no clause to end the recursion. I have not done this in a while but the correct conversion from centimeters to inches would be:

d / 2.54

As one of the answers in this previous thread What is a stack overflow error? says:

The common cause for a stack overflow is a bad recursive call.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
3

StackOverflow has nothing to do with conversion from double to int or float. It happens when there's a recursive call that does not have a base condition. A function keeps calling itself again and again and the stack frames keep building up until the time that there is no more space on the stack, thus causing a StackOverflow.

Make sure that your recursive functions have a base case.

Shobit
  • 776
  • 1
  • 6
  • 20
2
if (String.valueOf(d) != null && !String.valueOf(d).trim().isEmpty())

will always be true, so the method will keep calling itself, until it reaches a stack overflow error...

You need to find a condition that will break the recursion at some point.

And if you really want to convert centimeters to inches, it should probably look like:

private static double convertCentimeterToInches(double d) {
    return d / 2.54;
}
assylias
  • 321,522
  • 82
  • 660
  • 783
1

You have infinite recursion. To fix ...

Replace this

    feetPart = Math.floor(convertCentimeterToInches(d) / 12);
    inchesPart = Math.ceil(convertCentimeterToInches(d) - (feetPart * 12));

with this

    feetPart = Math.floor(d / 12);
    inchesPart = Math.ceil(d - (feetPart * 12));
xagyg
  • 9,562
  • 2
  • 32
  • 29