Pergunta

I'm having difficulties fixing my code. The error message I receive is:

Exception in thread "main" java.lang.NumberFormatException: For input string: "60 45 100 30 20" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Lab3.main(Lab3.java:15)

Here is my code:

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

I believe I've correctly parsed my double variables into string variables, and Eclipse shows no further errors upon compilation. Can anyone suggest a solution to this problem?

Foi útil?

Solução 2

You're missing the s in input.split("\\+");. Should be input.split("\\s+");. The way you're doing it won't split anything, leaving pVelocity to be "60 45 100 30 20", which is not a number, hence

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

Outras dicas

The input you are attempting to parse "60 45 100 30 20" is not a number.

Try splitting input on space and parse each element:

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

Perhaps you meant "\\s+" (one or more whitespace) instead of "\\+" (a single plus sign).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top