由于nextDouble()我得到一个异常,我怎么能避免这个?(I am getting an exception due to nextDouble(), how can I avoid this?

编程入门 行业动态 更新时间:2024-10-27 22:28:17
由于nextDouble()我得到一个异常,我怎么能避免这个?(I am getting an exception due to nextDouble(), how can I avoid this?)

我是一名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"); } } }

I'm an AP computer science and I am coding java. There is only a limited number of methods and classes that I can use. For instance I am not allowed to use hasNextLine() . This is the error it gives me once I enter the value for "how many euros is one dollar." 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"); } } }

最满意答案

尝试将其放入主方法中:

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

Try puting this into your main method instead:

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

更多推荐

本文发布于:2023-07-27 18:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1294011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   我怎么能   nextDouble   avoid   due

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!