由于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"); } }更多推荐
发布评论