1

As a beginner, i cannot really understand the issue in this code:

package Currency;

import java.util.Scanner;

public class Currency {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double value1 = 0.00, value2 = 0.00;
        String currency;

        System.out.print("Enter a value: ");
        value1 = input.nextDouble();
        System.out.print("USD or EUR: ");
        currency = input.nextLine();


        if(currency.equals("USD")){
            value2 = value1 * 0.734878047;
            System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
        } else if (currency.equals("EUR")) {
            value2 = value1 * 1.36077;
            System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
        } else {
            System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
            + "\nConversion rate: 1 EUR = 1.36077 USD");
        }

        input.close();

    }

}

Somehow, it doesnt even read the second input, but just prints last else. Can anyone help me to get this right? :)

Thank you in advance.

hungr
  • 2,086
  • 1
  • 20
  • 33
user3836013
  • 5
  • 1
  • 2
  • 4

3 Answers3

1

It seems like input.nextLine(); is reading in the newline from when the user hits enter after the first prompt.

A quick fix could be adding in another input.nextLine() "dummy" call before you prompt for USD or EUR:

package Currency;

import java.util.Scanner;

public class Currency {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double value1 = 0.00, value2 = 0.00;
        String currency;

        System.out.print("Enter a value: ");
        value1 = input.nextDouble();
        input.nextLine();
        System.out.print("USD or EUR: ");
        currency = input.nextLine();

        if(currency.equals("USD")){
            value2 = value1 * 0.734878047;
            System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
        } else if (currency.equals("EUR")) {
            value2 = value1 * 1.36077;
            System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
        } else {
            System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
                    + "\nConversion rate: 1 EUR = 1.36077 USD");
        }

        input.close();
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Nick Meyer
  • 1,771
  • 1
  • 17
  • 29
0

I think that hitting the return key is somewhat affecting the next readline. Doing a minor change as below seems to have fixed the problem:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double value1 = 0.00, value2 = 0.00;
    String currency;

    System.out.print("Enter a value: ");
    String str = input.nextLine();
    value1 = Double.parseDouble(str);
    System.out.print("USD or EUR: ");
    currency = input.nextLine();


    if(currency.equals("USD")){
        value2 = value1 * 0.734878047;
        System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
    } else if (currency.equals("EUR")) {
        value2 = value1 * 1.36077;
        System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
    } else {
        System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
        + "\nConversion rate: 1 EUR = 1.36077 USD");
    }

    input.close();
}

Example:

Enter a value: 25
USD or EUR: USD
25.0USD = 18.371951175 EUR. (Conversion rate: 1 USD = 0.734878047 EUR)

Alternatively, since in your case you are expecting a string without any white space, you could use the .next() property instead and leave your code as is:

   public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double value1 = 0.00, value2 = 0.00;
    String currency;

    System.out.print("Enter a value: ");
    value1 = input.nextDouble();
    System.out.print("USD or EUR: ");
    currency = input.next();


    if(currency.equals("USD")){
        value2 = value1 * 0.734878047;
        System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
    } else if (currency.equals("EUR")) {
        value2 = value1 * 1.36077;
        System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
    } else {
        System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
        + "\nConversion rate: 1 EUR = 1.36077 USD");
    }

    input.close();

}
npinti
  • 51,780
  • 5
  • 72
  • 96
-2

in the if clauses in both cases use currency.trim().equals(... to remove any whitespaces at the beginning or end of the String.

paubo147
  • 748
  • 4
  • 8