firechip
firechip

Reputation: 39

Java currencyConverter java.lang.NumberFormatException:

I get an error at this line double dollar = Double.parseDouble(input)

java.lang.NumberFormatException:
For input string: "q"(in sun.misc.FloatingDecimal)

Code:

   /**
   Converts money using CurrencyConverter
   */

   import java.util.*;

   public class CurrencyConverterTester {
     public static void main(String[] args) {
       //Normally the scanner is based on System.in,
       //Scanner scanner = new Scanner(System.in) ;
       //but for predictability we set the input to a fixed sequence:
       Scanner scanner = new Scanner("0.79447 100 20 88.88 q");

       System.out.print("Conversion factor (euros per dollar): ");
       //We could use scanner.nextDouble() here, but this is an example
       //of using parseDouble, which you need in the next loop.
       String input = scanner.next();
       double rate = Double.parseDouble(input);
       System.out.println(rate);
       //----------------Start below here. To do: approximate lines of code = 1
       // 1. make the CurrencyConverter object based on the rate
       CurrencyConverter converter = new CurrencyConverter(rate);
       //----------------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

       System.out.print("Dollar value (Q to quit): ");
       input = scanner.next();
       System.out.println(input); //echo the input
       //----------------Start below here. To do: approximate lines of code = 8
       // 1. write a while loop where the condition is that input is not "Q" or "q" ; 2. use parseDouble to get the dollars amount ; 3.  use the converter object to convert the dollars to euros ; 4.  print the dollars and euros in the style shown in the Expected region (Hint: use printf); 5. prompt for the next input ; 6. read the next input ; 7. echo the input (i.e., print it out)

       while (input != "q" && input != "Q") {
         double dollar = Double.parseDouble(input);
         double euros = converter.convert(dollar);
         System.out.printf("%.2f dollars = %.2f euros\n", dollar, euros);
         System.out.print("Dollar value (Q to quit): ");
         input = scanner.next();
         System.out.println(input);
       }
     }
   }

   Conversion factor(euros per dollar): 0.79447
   Dollar value(Q to quit): 100
   100.00 dollars = 79.45 euros
   Dollar value(Q to quit): 20
   20.00 dollars = 15.89 euros
   Dollar value(Q to quit): 88.88
   88.88 dollars = 70.61 euros
   Dollar value(Q to quit): q

Upvotes: 1

Views: 517

Answers (3)

Java Devil
Java Devil

Reputation: 10959

input is a String so you have to use .equals(), not ==. The equality operator == determines if two references point to the same object. Where the .equals() method checks if they are logically equal.

You also want to use || not &&. Even better would be to use String#equalsIgnoreCase()

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68857

Don't compare String values with == or !=. Use String.equals instead.

What you were doing with == is comparing the object addresses. This is not what you want, because you have a String object somewhere in memory that contains "q", and you have another object that contains "q", somewhere else in memory. These have different addresses, which causes the comparison using == returns false, while the contents was textually the same.

Upvotes: 0

Samuel O'Malley
Samuel O'Malley

Reputation: 3551

You are using the equality operator == for the String input. For Strings you must use their .equals() method.

Therefore:

while (input != "q" && input != "Q")

Should be:

while (!(input.equals("q") || input.equals("Q")))

or

while (!"q".equalsIgnoreCase(input)) // While input not q or Q

Also be careful using scanner.next() it would be better to use scanner.nextLine() otherwise you will get a newline character in your input variable. So when you type "q" the input variable might be String input = "q\n";

Upvotes: 2

Related Questions