2014-02-22 75 views
0

我無法修復我的代碼。收到錯誤信息是:在java的 「60 45 100 30 20」 在 sun.misc.FloatingDecimal.readJavaFormatString(未知來源) :錯誤:java.lang.NumberFormatException

異常在線程 「主」 java.lang.NumberFormatException:對於輸入字符串.lang.Double.parseDouble(來源不明) 在Lab3.main(Lab3.java:15)

這裏是我的代碼:

import java.io.*; 
import java.math.*; 

public class Lab3 
{ 
    public static void main(String[] args) throws IOException 
    { 
     InputStreamReader isr = new InputStreamReader(System.in); 
     BufferedReader br = new BufferedReader(isr); 
     double pVelocity, pAngle, tDistance, tSize, tElevation; 
     double radians, time, height, pDistance; 
     String input = br.readLine(); 

     String[] values = input.split("\\+"); 
     pVelocity = Double.parseDouble(values[0]); 
     pAngle = Double.parseDouble(values[1]); 
     tDistance = Double.parseDouble(values[2]); 
     tSize = Double.parseDouble(values[3]); 
     tElevation = Double.parseDouble(values[4]); 

     while(pVelocity != 0 && pAngle != 0 && tDistance != 0 && tSize != 0 && tElevation != 0) 
     { 
      radians = pAngle*(Math.PI/180); 
      time = tDistance/(pVelocity*Math.cos(radians)); 
      height = (pVelocity*time*Math.sin(radians))-(32.17*time*time)/2; 
      pDistance = time*(pVelocity*Math.cos(radians)); 

      if(pVelocity*Math.cos(radians) == 0) 
       System.out.print("The computed distance cannot be calculated with the given data."); 
      else if(height > tElevation && height <= tSize) 
       System.out.print("The target was hit by the projectile."); 
      else if(height < tElevation) 
       System.out.print("The projectile was too low."); 
      else if(height > tSize) 
       System.out.print("The projectile was too high."); 
      else if(pDistance < tDistance) 
       System.out.print("The computed distance was too short to reach the target."); 
      else if(height < 0) 
       System.out.println("The projectile's velocity is " + pVelocity + "feet per second."); 
       System.out.println("The angle of elevation is " + radians +"degrees."); 
       System.out.println("The distance to the target is " + tDistance + "feet."); 
       System.out.println("The target's size is " + tSize + "feet."); 
       System.out.println("The target is located " + tElevation + "feet above the ground."); 
     } 
    } 
} 

我相信我已經正確地分析我的雙變量轉換爲字符串變量,並且Eclipse在編譯時不顯示更多錯誤。任何人都可以提出解決這個問題?

回答

3

您錯過了sinput.split("\\+");。應該是input.split("\\s+");。你這樣做是不會分裂一切的好辦法,讓pVelocity"60 45 100 30 20",這是不是一個數字,因此

java.lang.NumberFormatException: For input string: "60 45 100 30 20" 
+0

謝謝你的幫助!這解決了它! – dabad

4

您正試圖解析"60 45 100 30 20"輸入不是一個數字。空間

嘗試拆分輸入和分析每一個元素:

for (String s : input.split(" ")) { 
    // parse s 
} 

也許你的意思"\\s+"(一個或多個空格),而不是"\\+"(一個加號)。

+0

什麼是令人尷尬的錯誤......是的,這解決了這個問題!非常感謝! – dabad

相關問題