-3

I'm trying to limit userinput only to a single letter of "abcd".

Working code in C:

printf("Which currency do you want to exchange ?\n");
printf("     (a) EUR     (b) USD     (c) GBP     (d) CNY \n");
char input, input2, icurrency [3], ocurrency [3], output;
scanf(" %c", &input);

//While loop to limit user input to "abcd" only for starting currency input
while (input != 'a' && input != 'b' && input != 'c' && input != 'd')
{
    printf("\n\nWrong input, try again !\n");
    printf("     (a) EUR     (b) USD     (c) GBP     (d) CNY \n");
    scanf(" %c", &input);
}

Same logic in Java causes an infinite loop despite the right input

System.out.print("Which Currency do you want to convert ?\nEUR (a)   USD (b)   GBP    
(c)   CNY (d)\nChoice:  ");
Scanner input = new Scanner (System.in);
String iCurrency = input.next();
while (iCurrency != "a" && iCurrency != "b" && iCurrency != "c" && iCurrency != "d")
    {
        System.out.print("\nWrong input, try again !\nEUR (a)  USD (b)  GBP (c)  CNY 
        (d)\nChoice:  ");
        iCurrency = input.next();
    }
input.close();

What am I missing here ? Why is the Java Code causing an infinte loop, thanks in advance

Sherif K
  • 1
  • 1

0 Answers0