2014-02-22 3 views
0

내 코드를 수정하는 데 어려움이 있습니다. I 나타나는 오류 메시지는 다음 스레드에 "메인"에 java.lang.NumberFormatException의Error : java.lang.NumberFormatException

예외 : 입력 문자열 : sun.misc.FloatingDecimal.readJavaFormatString (알 소스) 에서 "60 45 100 30 20"에서 자바 내가 제대로 문자열로 내 더블 변수를 분석 한 생각

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."); 
     } 
    } 
} 

: Lab3.main에서 .lang.Double.parseDouble (알 수없는 소스) (Lab3.java:15는) 다음

내 코드입니다 변수를 포함하며, Eclipse는 컴파일시 오류를 표시하지 않습니다. 누구든지이 문제에 대한 해결책을 제시 할 수 있습니까?

답변

3

s이 (가) input.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