0

I am trying to output NaN in string format when x is NaN. However, the console throws up String cannot be converted to double error when I am trying to return string of "NaN" when Double.isNaN is true. I tried parsing string r as a double using Double.parseDouble() but to no avail.

Here is my code:

public static double heaviside(double x) {
    String r = "NaN";
    r = Double.parseDouble(r);
    double result;
    if (Double.isNaN(x)) return r;
    else if (x < 0.0) return result = 0.0;
    else if (x == 0.0) return result = 0.5;
    else return result = 1.0;
}

Console output

ActivationFunction.java:6: error: incompatible types: double cannot be converted to String r = Double.parseDouble(r); ^ ActivationFunction.java:8: error: incompatible types: String cannot be converted to double if (Double.isNaN(x)) return r; ^

2
  • 3
    And if x is NaN, why don't you just return x? Commented May 27, 2020 at 7:13
  • I suggest you using an IDE like IntelliJ or Eclipse. They are very helpful when learning a new language. Commented May 27, 2020 at 7:37

3 Answers 3

3

You problem comes from the fact, that you try to assign double to String variable. r is defines as String and Double.parseDouble(r) returns double. It will work if you assing it to result instead, like so: double result = Double.parseDouble(r);

However, there is no need to parse new double from String in the first place. You want to return NaN when x == NaN. You can just return x in that case because, well, x is NaN. No need for another variable, parsing and all of that.

Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to convert String into double and storing its value in r which is a type of String.

You should create another variable of double type and can store that value in that like this:

public static double heaviside(double x) {
    String r = "NaN";
    double d = Double.parseDouble(r);
    double result;
    if (Double.isNaN(x)) return d;
    else if (x < 0.0) return result = 0.0;
    else if (x == 0.0) return result = 0.5;
    else return result = 1.0;
}

I hope it will help you. Happy coding..!

3 Comments

The question is, what is the point of all of this? "If x is NaN, create a new double representing NaN and return that new value". Why don't just return x in the first place?
You are right. I give this much explanation because @palerabbit can understand that he was trying to store double into String and that is an actual error that he mentioned into question.
Thanks for the input. I was trying to get "NaN" in my output instead of infinity but yes what you have here makes alot of sense! cheers @Amongalen
0

"NaN" is not a double, you can't convert it to double and not return x because Java returns an exception on line 2 of your code and does not continue to run the rest of the code

4 Comments

Funny enough, I've just tried double test = Double.parseDouble("NaN"); and... it runs just fine.
The error in that line is not that "NaN" is not a double. But that parseDouble returns a double and not a String, so you cannot assign it to a String.
I agree with you @Amongalen, I run that too and it works fine not sure why. But I think, I tried to print in 1System.out.println(Double.parseDouble("NaN))` which converts double into String again.
Compilation errors are not exceptions, and neither compilation errors nor exceptions are returned. Don't misuse standard terminology.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.