2014-11-06 77 views
1

我試圖解析StringDouble和由於變量的限制,我必須以非常奇怪的方式做到這一點。目前我遇到的問題是具有兩個命名相同的功能,但一個需要Double,另一個需要double如何在解析字符串時獲得Double而不是Double?

public class Calculator { 
      Double x; 

      public Double x(String x){ 
       //code ommited 
       x(Double.parseDouble(x.substring(x.lastIndexOf(" ") + 1, x.length()))); 
       //^^ attempting to get this variable to be a Double instead of double 

       return new Double(x); 
      } 

      public Double x(Double x){ 
       //code ommited 
       return new Double(0); 
      } 


      public Double x(double x){ 
       //code ommited 
       return new Double(0); 
      } 

    } 
+3

'new Double(String)'會將'String'轉換爲'Double'對象。 – khelwood 2014-11-06 19:50:47

+1

不支持自動換行和解包?傳遞雙精度值應該代替雙精度值 – JClassic 2014-11-06 20:02:10

回答

5

而不是使用Double.parseDouble,使用Double.valueOf(String),它會返回一個雙。

順便說一句,使用Double.valueOf(String)相當於new Double(Double.parseDouble(String))

相關問題