由于nextDouble()我得到一个异常,我怎么能避免这个?

时间:2015-11-06 04:38:07

标签: java

我是一名AP计算机科学,我正在编写java。我可以使用的方法和类数量有限。例如,我不允许使用hasNextLine()。一旦我输入“多少欧元是一美元”的值,这就是它给我的错误。它允许我输入该值,然后要求输入美元值。但是,在我输入之前,此错误会显示出来:

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)   
at java.lang.Double.parseDouble(Double.java:538)
at CurrencyConverter.main(CurrencyConverter.java:20)
public class Currency
{
    private double rate;

    public Currency()
    {
        rate = 0.0;
    }

    public Currency(double newRate)
    {
        rate = newRate;
    }

    public double convert(double dollar)
    {
        double euro = dollar * rate;
        return euro;
    }
}
import java.util.Scanner;
import java.util.Scanner;
public class CurrencyConverter
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("How many euros is one dollar?");
        double exchangerate = input.nextDouble();
        System.out.println("Dollar value (Q to quit):");
        String dollarvalue = input.nextLine();
        double dv = 0.0;
        String bvalue = "";
        String bvaluetwo = "Q";
        if (dollarvalue.equals(bvalue))
        {
            dollarvalue = "test";
        }
        else if (!dollarvalue.equals(bvaluetwo))
        {   
            dv = Double.parseDouble(dollarvalue);
        } 

        Currency exchange = new Currency(exchangerate);

        while (dollarvalue != "Q")
        {
            double eurovalue = exchange.convert(dv);
            System.out.println(dv + " dollar = " + eurovalue + " euro");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

double exchangerate = input.nextDouble();如果没有输入,这会给你例外,在调用任何hasNext()方法之前总是使用scanner next方法

这是你的主要罪魁祸首:

   System.out.println("Dollar value (Q to quit):");
            String dollarvalue = input.nextLine();
            double dv = 0.0;
            String bvalue = "";
            String bvaluetwo = "Q";
            if (dollarvalue == bvalue) {
                dollarvalue = "test";
            } else if ((dollarvalue != bvaluetwo)) {
                dv = Double.parseDouble(dollarvalue);
 }

您正在存储String“Q”或用户输入的其他字符串值 在dv = Double.parseDouble(dollarvalue);中,您正在将其解析(转换)为double,这会导致java.lang.NumberFormatException异常

答案 1 :(得分:0)

尝试将其放入主方法中:

        public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("How many euros is one dollar?");
        double exchangerate = input.nextDouble();
        //You have to put this in order to continue
        input.nextLine();
        System.out.println("Dollar value (Q to quit):");
        String dollarvalue = input.nextLine();
        double dv = 0.0;
        String bvalue = "";
        String bvaluetwo = "Q";
        if (dollarvalue == bvalue)
        {
            dollarvalue = "test";
        }
        else if ((dollarvalue != bvaluetwo))
        {   
            dv = Double.parseDouble(dollarvalue);
        } 

        Currency exchange = new Currency(exchangerate);

        while (!dollarvalue.equals("Q"))
        {
            double eurovalue = exchange.convert(dv);
            System.out.println(dv + " dollar = " + eurovalue + " euro");
            System.out.println("Dollar value (Q to quit):");
            dollarvalue = input.nextLine();
             if (!dollarvalue.equals("Q")) {     
             dv = Double.parseDouble(dollarvalue);
             eurovalue = exchange.convert(dv);
                System.out.println(dv + " dollar = " + eurovalue + " euro");
            } 

        }
        System.out.println("You pressed Q, have a nice day");
    }
}