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