Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not being able to compile

 
Tontang Bei
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class q16{
public static void main(String[] args){
String Str="1.1";
double doubleVal;

try{
doubleVal=Double.parseDouble(Str);
}
catch(Exception e){}
System.out.println(doubleVal);
}
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15208
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next time, please explain what the error message is that you get instead of just posting the code without any comments at all.

You're probably getting an error message that says the variable doubleVal may not have been initialized.

Look at your code. What happens when Double.parseDouble(Str) throws an exception? Execution goes to the catch block, but the variable doubleVal remains uninitialized. In Java, you must initialize local variables before you refer to them (as you do in the System.out.println(...) statement at the bottom.
 
Tontang Bei
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but even when I initialize I am getting error.


q16.java:9: cannot find symbol
symbol : method parseDouble(java.lang.String)
location: class Double
doubleVal=Double.parseDouble(Str);
^
1 error

Am I using method incorrectly?
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I change this line:

double doubleVal = 0.0;

your program works fine for me. Have you ever been able to compile any programs?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tontang --

I bet you've written a class named "Double" at some point, and your file "Double.class" is still hanging around somewhere. Delete it, and try not to name your own classes the same as common Java API class names!
 
Tontang Bei
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Mr. Hill. You are a Genius!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic