0

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");
        }
    }
}
kay
  • 371
  • 4
  • 18
  • Comparing `String` `dollarvalue == bvalue` with `==`? – Apurva Nov 06 '15 at 04:42
  • ^ thats a problem you have but also... try putting in a `nextLine()` in there to clear it out. – 3kings Nov 06 '15 at 04:42
  • And put `Double.parseDouble()` in `try catch` block, it may throw `NumberFormatException` – Apurva Nov 06 '15 at 04:45
  • From the error it sounds like you are not entering a number for the exchange rate but instead pressing the enter key... – cjds Nov 06 '15 at 04:46
  • no i am not entering anything and the error shows up and so should i use the equals method? – kay Nov 06 '15 at 04:46
  • @KeyanKazemian error shows up because " i am not entering anything" ,`double exchangerate = input.nextDouble();` expects you to enter a double value. – Ramanlfc Nov 06 '15 at 05:02
  • no you dont understand it doesn't give the opportunity to enter anything @Ramanlfc – kay Nov 06 '15 at 05:06
  • Are you allowed to use nextLine()? – Sekula1991 Nov 06 '15 at 05:14
  • yes but i'm already using it. where else should i use it?? @Sekula1991 – kay Nov 06 '15 at 05:15
  • Have in mind, that even if you get into while method, you will have endless loop. Do you want to have your program check for dollar value until Q is pressed? – Sekula1991 Nov 06 '15 at 05:15

2 Answers2

0

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

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); ,you are parsing(converting) it into a double which is throwing java.lang.NumberFormatException exception

Ramanlfc
  • 7,900
  • 1
  • 14
  • 24
0

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");
    }
}
Sekula1991
  • 278
  • 4
  • 16
  • THANKS! can you explain to me what you did? – kay Nov 06 '15 at 05:37
  • First you had to put input.nextLine(); below inputNextDouble(); , then i fixed your while loop because your previous loop was endless, in the while loop you get another input, and if its "Q" it just passes through, otherwise it parses your double value, and so on until you press "Q"- which was i guess your intention with that code of yours. :) – Sekula1991 Nov 06 '15 at 05:41
  • If you have questions with nextLine(), read this: http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Sekula1991 Nov 06 '15 at 05:43
  • i see, is there any part of it that i can take out now? – kay Nov 06 '15 at 05:43
  • Well, i dont see a point of `bvalue` and `bvaluetwo` but thats up to you. – Sekula1991 Nov 06 '15 at 05:45