[英]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.