-1

Somehow this code of mine is not working. Its giving me : error, cant find symbol celsius = Double.parseDouble(s); symbol: method parseDouble(String location: class Double

It's really weird because it works with Integer.parseInt, but not Double.parseDouble somehow?? Any suggestions?

class CelsiusTry{
    public static void main(String[]args){
        String s;
        double celsius, fahrenheit;

        s = JOptionPane.showInputDialog("Enter Celsius");
        celsius = Double.parseDouble(s);
        fahrenheit = celsius * 1.8 + 32;

        JOptionPane.showMessageDialog(null, "Fahrenheit is: " + fahrenheit);    
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Zerenity
  • 107
  • 6
  • Does `java.lang.Double.parseDouble(s);` work? If yes then you are facing same problem as described in point 4.6 at accepted answer in [What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?](https://stackoverflow.com/q/25706216). – Pshemo Feb 25 '20 at 00:21
  • Hmm interesting, it did indeed work, however, the interesting aspect is that I have not created a class called "Double", nor have a declared a constant variable to be Double. this is weird – Zerenity Feb 25 '20 at 00:27
  • Example you provided works fine by itself. If you are facing some problem then reason for it is not in this code but in its *surroundings*. We can't tell what exactly is going on but most often such problem is caused by having different class with same name which indeed doesn't have method you want to call (compiler doesn't lie about problem it faces, it just reports it). – Pshemo Feb 25 '20 at 00:32
  • Haha right on, you pinpointed the exact problem to this error of mine lol. Yes, it was the surroundings, I had named a class to Double far back lol. Removed that class from another program thank you. – Zerenity Feb 25 '20 at 00:36
  • Hey why are you downvoting my question, Im new to this forum and wanted a specific answer to the question of mine, I would never be able to pinpoint the problem by myself by reading the entire link of yours... – Zerenity Feb 25 '20 at 00:39
  • I wasn't the one who downvoted your question, I only marked it as duplicate of other question which already explains this problem and its potential causes (among which there is also your situation). – Pshemo Feb 25 '20 at 01:43

1 Answers1

2

You have your own class named Double which is shadowing java.lang.Double; rename or remove it or use the fully qualified class name. Also, prefer declaring and initializing your variables in the narrowest possible scope. Like,

String s = JOptionPane.showInputDialog("Enter Celsius");
double celsius = java.lang.Double.parseDouble(s);
double fahrenheit = celsius * 1.8 + 32;

JOptionPane.showMessageDialog(null, "Fahrenheit is: " + fahrenheit);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • This did indeed work, however, I did not quite understand why this occured. "you have my own class named double which is shadowing java.lang.Double; rename or remove or use the fully qualified class name". What do you mean by that? I dont have other classes named double – Zerenity Feb 25 '20 at 00:25
  • There is another `Double.class` on your classpath (remove that `Double.class` or use `java.lang.Double` to specify which `Double`). – Elliott Frisch Feb 25 '20 at 00:27
  • Wow, you are the man, thanks! I have now learned not to name classes as reserved java variables (really a noob mistake). – Zerenity Feb 25 '20 at 00:33