Currently, you cannot switch to String . The language specification clearly shows that you can switch on:
SwitchStatement: switch ( Expression ) SwitchBlock
The Expression type must be char , byte , short , int , Character , byte , short , Integer or enum , or a compile-time error occurs.
Depending on what you want to do, you can switch on each char String :
String s = "Coffee, tea, or me?"; int vowelCount = 0; int punctuationCount = 0; int otherCount = 0; for (char letter : s.toUpperCase().toCharArray()) { switch (letter) { case 'A': case 'E': case 'I': case 'O': case 'U': vowelCount++; break; case ',': case '.': case '?': punctuationCount++; break; default: otherCount++; } } System.out.printf("%d vowels, %d punctuations, %d others", vowelCount, punctuationCount, otherCount );
polygenelubricants
source share