wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Not being able to compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not being able to compile" Watch "Not being able to compile" New topic
Author

Not being able to compile

Tontang Bei
Ranch Hand

Joined: Oct 21, 2006
Posts: 130
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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tontang Bei
Ranch Hand

Joined: Oct 21, 2006
Posts: 130
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

Joined: Sep 26, 2006
Posts: 148
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Tontang Bei
Ranch Hand

Joined: Oct 21, 2006
Posts: 130
Thankyou Mr. Hill. You are a Genius!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Not being able to compile