-1

I am writing a program about exchange transactions between different currencies.
At a certain point the program asks the user to input the initial currency and check if the string input by the user is within the values specified by the program:

while (!isSet) {
    System.out.println("Give the Initial Currency:  USD | EUR | GBP | JPY | CHF | CAD | AUD");
    start_currency = scanner2.nextLine();

    if ((start_currency != "USD") && (start_currency != "EUR")
        && (start_currency != "GBP") && (start_currency != "JPY")
        && (start_currency != "CHF") && (start_currency != "CAD")
        && (start_currency != "AUD")
    ) {
        System.out.println("Please choose a value from the initial currencies specified above!");
    } else {
        isSet = true;
    }
}

The program passes through this block of code without being able to execute it (as if the condition is always false).

Can anyone pinpoint my error in the aforementioned part of the code?

JustCarty
  • 3,839
  • 5
  • 31
  • 51
  • Maybe use something like this to check if `start_currency` is in the array: https://stackoverflow.com/a/1128728/3578036 – JustCarty Oct 10 '18 at 08:41

1 Answers1

2

I think your problem is with the logical operators.

What you basically are doing is:

¬(a and b)=(not a) or (not b)

So you would need to change all your AND Operators to OR operators.

-> De Morgan's laws

Simon
  • 81
  • 5