错误:java.lang.NumberFormatException

错误:java.lang.NumberFormatException,java,parsing,io,numberformatexception,Java,Parsing,Io,Numberformatexception,我在修改代码时遇到困难。我收到的错误消息是: 线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“60 45 100 30 20” 位于sun.misc.FloatingDecimal.readJavaFormatString(未知源) 位于java.lang.Double.parseDouble(未知源) 位于Lab3.main(Lab3.java:15) 这是我的密码: import java.io.*; import java.math

我在修改代码时遇到困难。我收到的错误消息是:

线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“60 45 100 30 20” 位于sun.misc.FloatingDecimal.readJavaFormatString(未知源) 位于java.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.");
        }
    }
}
import java.io.*;
导入java.math.*;
公共类Lab3
{
公共静态void main(字符串[]args)引发IOException
{
InputStreamReader isr=新的InputStreamReader(System.in);
BufferedReader br=新的BufferedReader(isr);
双pVelocity、pAngle、tDistance、tSize、tElevation;
双弧度、时间、高度、距离;
字符串输入=br.readLine();
String[]value=input.split(\\+);
pVelocity=Double.parseDouble(值[0]);
pAngle=Double.parseDouble(值[1]);
tDistance=Double.parseDouble(值[2]);
tSize=Double.parseDouble(值[3]);
tElevation=Double.parseDouble(值[4]);
而(pVelocity!=0&&pAngle!=0&&tDistance!=0&&tSize!=0&&tElevation!=0)
{
弧度=pAngle*(数学PI/180);
时间=tDistance/(pVelocity*数学cos(弧度));
高度=(pVelocity*时间*数学sin(弧度))-(32.17*时间*时间)/2;
pDistance=时间*(pVelocity*数学cos(弧度));
如果(pVelocity*数学cos(弧度)==0)
System.out.print(“无法使用给定数据计算计算出的距离”);
否则如果(高度>目的地和高度大小)
系统输出打印(“射弹太高了。”);
否则如果(pDistance

我相信我已经正确地将我的双变量解析为字符串变量,Eclipse在编译时没有显示更多的错误。有人能提出解决这个问题的办法吗

您缺少
input.split(\\+)中的
s
。应该是
input.split(\\s+)。您这样做不会分割任何内容,将
pVelocity
保留为
“60 45 100 30 20”
,这不是一个数字,因此

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

您试图解析的输入不是数字

尝试在空间上拆分输入并分析每个元素:

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

也许你的意思是
“\\s+”
(一个或多个空格)而不是
“\\+”
(一个加号)。

多么令人尴尬的错误……是的,这解决了这个问题!非常感谢你!谢谢你的帮助!这就解决了它!