just to clarify this is hw.
In a project we’re doing, a user isn’t allowed to enter numbers or special characters (i.e ! @ £ etc)
char letter;
String phonetic;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = letter = kb.next().charAt(0);
switch(Character.toUpperCase(letter))
{
case 'A':
{
Dot();
Dash();
Red();
}
break;
case '1,2,3,4,5,6,7,8,9,0':
{
System.out.println('No number input please!');
}
break;
}
The error is on
'1,2,3,4,5,6,7,8,9,0'
Eclipse says
invalid character constant
Isn’t it really long winded if I have to enter all the numbers manually?
i.e. case ‘1’: case ‘2’:
even with
case 1,2,3,4,5,6,7,8,9,0:
It won’t work.
Is there an shorter way to do this using switch statements?
Thank you!
Its because Case expression should be an
int-compatible literalor aStringfrom java 7.character literals are represented using single quotes.
c, it should only be of one length, while your case doesn’t reflect that, thus the error.If you just wanna check if the character is only alpha, then use Charcter#isDigit(char) or Charcter#isLetter before the switch starts like in below code: