簡體   English   中英

錯誤:java.lang.NumberFormatException

[英]Error: java.lang.NumberFormatException

我在修復代碼時遇到困難。 我收到的錯誤消息是:

線程“主”中的異常java.lang.NumberFormatException:對於輸入字符串:Lab3處java.lang.Double.parseDouble(未知源)處sun.misc.FloatingDecimal.readJavaFormatString(未知源)處的輸入字符串為“ 60 45 100 30 20”。主要(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.");
        }
    }
}

我相信我已經將我的double變量正確地解析為字符串變量,並且Eclipse在編譯時沒有顯示更多錯誤。 誰能建議解決這個問題的方法?

您嘗試解析"60 45 100 30 20"的輸入不是數字。

嘗試在空間上分割輸入並解析每個元素:

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

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

您在input.split("\\\\+");缺少s input.split("\\\\+"); 應該是input.split("\\\\s+"); 您執行此操作的方式不會拆分任何內容,而將pVelocity"60 45 100 30 20" ,這不是數字,因此

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM