简体   繁体   English

由于nextDouble()而出现异常,如何避免这种情况?

[英]I am getting an exception due to nextDouble(), how can I avoid this?

I'm an AP computer science and I am coding java. 我是AP计算机科学,正在编写Java。 There is only a limited number of methods and classes that I can use. 我只能使用有限的方法和类。 For instance I am not allowed to use hasNextLine() . 例如,不允许我使用hasNextLine() This is the error it gives me once I enter the value for "how many euros is one dollar." 这是我输入“ 1美元能换多少欧元”的值时给我的错误。 It allows me to enter that value and then asks to enter the dollar value. 它允许我输入该值,然后要求输入美元值。 However, before I can enter it, this error shows up: 但是,在输入之前,会出现此错误:

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

double exchangerate = input.nextDouble(); this will give you exception if there is no input , always use scanner's hasNext() method before calling any of the next methods 如果没有输入,这将为您提供异常,请始终在调用任何next方法之前使用扫描仪的hasNext()方法

Here is your main culprit : 这是您的主要罪魁祸首:

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

You are storing a String of "Q" or other string values entered by user , then in dv = Double.parseDouble(dollarvalue); 您要存储String “ Q”或用户输入的其他字符串值,然后存储在dv = Double.parseDouble(dollarvalue); ,you are parsing(converting) it into a double which is throwing java.lang.NumberFormatException exception ,您正在将其解析(转换为double精度),从而抛出java.lang.NumberFormatException异常

Try puting this into your main method instead: 尝试将其放入您的main方法中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用扫描仪执行 nextDouble() ,但对于字符串而不是双精度值? - How can I do nextDouble() with a scanner, but for a string instead of a double? 收到异常“ SSLPeerUnverifiedException:对等方未通过身份验证”,如何避免/忽略它? - Getting an exception “SSLPeerUnverifiedException: peer not authenticated”, how can I avoid/ignore it? 我如何避免在运行石英schedular.start()时收到的异常 - How can i avoid the exception that i am receiving while running quartz schedular.start() 如何避免stackoverflow异常 - How can I avoid stackoverflow exception 由于AJax调用过多,如何在Java的Selenium Web驱动程序中对其发痒,我的仪表板页面上出现异常 - i am getting an exception on my dashboard page due to AJax huge calls how to tickle it in selenium web driver with java 是否由于类型不匹配而导致此错误? 将来如何避免这种情况? - Is this error due to type mismatch? How can I avoid this in the future? 在我需要重新设定之前,我可以使用randomGenerator.nextDouble()多少次? - How many times can I use randomGenerator.nextDouble() before I need to reseed? 我为什么要例外? - Why am I getting an exception? 我正在发生致命异常 - I am getting FATAL EXCEPTION 为什么会出现此异常? - Why am I getting this exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM