-2

I'm wondering if I can compare a char with 2 values? I'm trying to make a loop that will repeat until some variable is equal to either A, a, B, b, C, or c. Is there any way to ignore the capitalization?

I tried the following code:

 final char choiceA = 'a'|'A';
 final char choiceB = 'b'|'B';
 final char choiceC = 'c'|'C';

When I tried it it works only when I put a, b or c. If I put a capital letter the loop keeps going on.

  • 1
    https://stackoverflow.com/questions/31335472/assigning-more-than-one-character-in-char similar with this – stephen Oct 22 '19 at 01:06
  • 2
    Perhaps this will help: https://stackoverflow.com/questions/10223176/how-to-compare-character-ignoring-case-in-primitive-types :) – Anil Oct 22 '19 at 01:08
  • 1
    XY problem, I fixed the question to highlight OPs expectations instead of his attempted solution. –  Oct 22 '19 at 01:19
  • 2
    Closely related - https://stackoverflow.com/q/234591 – Dawood ibn Kareem Oct 22 '19 at 01:44

1 Answers1

0

No. A character cannot be both lowercase and uppercase, but you can ignore case when you compare characters (by converting your input to upper/lower case). E.g.:

char c = Character.toLowerCase(choice);
switch (c) {
     case 'a':
       // choice is either 'a' or 'A'
     case 'b':
       // choice is either 'b' or 'B'
     case 'c':
       // choice is either 'c' or 'C'
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • what is the meaning of the // ... a? If I do toUpperCase wouldnt it ignore the lower case a and only accept A? – John Jaure Oct 22 '19 at 01:21
  • @JohnJaure see the edited content now. FWIW, this is a typical case for `switch`; also, AFAIR, the usual idiom is to convert to lowercase, not uppercase –  Oct 22 '19 at 01:22
  • 4
    The "//" is a comment. Can I suggest that you should be do a Java tutorial before you start trying to write code? It will save you a lot of hair-tearing. It is best to learn the capabilities of the Java language rather than guess them by experimenting with the syntax. – Stephen C Oct 22 '19 at 02:22