0

I was working on this simple currency converter just to reinforce some concepts I learn. Everything is working as planned except after the converter runs the first time, the second iteration of the while loop causes the first answer to use both the if and the else. The output is as follows:


Convert US dollar into which currency? [Yen, Yuan, Peso, Pound, Euro] Pound

How many dollars would you like converted? 5

You have converted $5.0 into 3.8 Pound

Convert US dollar into which currency? [Yen, Yuan, Peso, Pound, Euro] Please select a currency Convert US dollar into which currency? [Yen, Yuan, Peso, Pound, Euro]


Currency class:

package currency;

import java.util.HashMap;
import java.util.Map;

public class Currency {

    private Map<String, Double> ct;
    private double newCurrency;
    private String answer;
    private double amount;

    public Currency() {
        this.ct = new HashMap<String, Double>();
    }

    // Getters and Setters
    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getAnswer() {
        return this.answer;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getAmount() {
        return this.amount;
    }

    // Current available currencies
    public void loadMap() {
        ct.put("Pound", 0.76);
        ct.put("Euro", 0.85);
        ct.put("Yen", 112.23);
        ct.put("Yuan", 6.61);
        ct.put("Peso", 18.87);

        System.out.println(ct.keySet());

    }

    // Multiples amount times currency
    public double multiplyCurrency(String answer, double amount) {

        newCurrency += ct.get(getAnswer()) * getAmount();

        return newCurrency;

    }

    public double getNewCurrency() {
        return this.newCurrency;
    }

    // Checks user if Map contains userinput.
    public boolean checkAnswer(String answer) {
        if (!ct.containsKey(answer)) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    public String toString() {
        return "You have converted $" + getAmount() + " into " + getNewCurrency() + " " + this.answer + "\n";
    }

}

Logic class:

package logic;

import java.util.Scanner;

import currency.Currency;

public class Logic {

    private Currency currency;
    private Scanner reader;
    private boolean running;
    private String answer;

    public Logic() {
        this.currency = new Currency();
        this.reader = new Scanner(System.in);
        this.running = true;
        this.answer = null;
    }

    public void start() {

        //Runs until quit
        while (running) {
            System.out.println("Convert US dollar into which currency?");
            currency.loadMap();
            answer = reader.nextLine();
            currency.setAnswer(answer);

            // checks to make sure the value is in the map
            if (currency.checkAnswer(currency.getAnswer()) == true) {
                System.out.println("\n" + "How many dollars would you like converted?");
                double amount = reader.nextDouble();
                currency.setAmount(amount);
                currency.multiplyCurrency(currency.getAnswer(), currency.getAmount());
                System.out.println("");
                System.out.println(currency);

            } else {
                System.out.println("Please select a currency");
            }

        }
    }
}

Main Class:

package logic;

public class Main {

    public static void main(String[] args) {

        Logic l = new Logic();

        l.start();


    }

}

I would really appreciate any feedback so I can make note and and never have this happen again. Thanks in advance.

S.L. Barth
  • 7,954
  • 71
  • 47
  • 62
Dyeh310
  • 5
  • 2

2 Answers2

0

In your Currency class, just change answer = reader.nextLine(); to answer = reader.next();

It's due to this - i.e. causing a new line on nextDouble();

Also, to note that if the user doesn't type in the exact text of the currencies, it will just print your else statement and start again.

achAmháin
  • 3,944
  • 3
  • 12
  • 39
0

The Scanner.nextDouble method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner.nextLine. You're basically suffering from the same issue as noted in this post

A simple work around would be to read the value as a String and parse it to double, so to replace

double amount = reader.nextDouble();

by

double amount = Double.parseDouble(reader.nextLine());
Filip
  • 827
  • 8
  • 18
  • I appreciate all the feedback. This answer worked perfect. I never knew this and will keep it in mind for all my future programs. Happy Coding! – Dyeh310 Oct 21 '17 at 02:37