Multiple characters in a switch statement?

梦想的初衷 提交于 2019-12-13 09:28:14

问题


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!


回答1:


Its because Case expression should be an int-compatible literal or a String from java 7.

case '1,2,3,4,5,6,7,8,9,0':

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.

'1,2,3,4,5,6,7,8,9,0'  this is not a legal character.

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:

char ch=  (Character.toUpperCase(letter);
if(!Character.isDigit(ch)) {
    switch(Character.toUpperCase(letter))
     {
       case 'A':
            {
                Dot();
                Dash();
                Red();
            }
            break;
        }
     }
else {
       System.out.println("no numbers please")
}



回答2:


There's no easier way using case, what about?:

if ('0' <= letter && letter <= '9')
  System.out.println('No number input please!');



回答3:


Isn't it really long winded if I have to enter all the numbers manually?

Yes.

Is there an shorter way to do this using switch statements?

No.

Consider an if statement instead...




回答4:


No, Java in this situation is not smart like C#. You need to write multiple lines for that. If you want to compare strings you need to use if statements. Also remember to use this code for comparision:

if("search".equals(string2)) {...}

You cannot compare by == this would only compare the memory addresses. Also note that I use the equals on the static string and not on the variable string2 because you code would break if string2 is null.




回答5:


I hope this one enlighten you more.

Consider, your expression generates output as A , B, C, D, E, F, G, H, I, ...till Z. and you want to execute same method/function for all them.

Then, you can check the ascii values of the characters and modify your code to use if and for loop or else use switch as mentioned in the program in following example program.

Play around with code to learn more.

public class SwitchClass 
{   

public void method1()
{
        System.out.println("Menthod 1");
}

public void method2()
{
        System.out.println("Menthod 2");
}

public void method3()
{
        System.out.println("Menthod 3");
}   

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    SwitchClass sw = new SwitchClass();
    System.out.println("Enter the String:");
    String input = in.next();

    for(int i = 0; i<input.length(); i++)
    {

        switch(Character.toUpperCase(input.charAt(i)))
        {

            case 'A':
            case 'B':
            case 'C':
            case 'U':
                    System.out.println(Character.toUppercase(input.charAt(i))+" Case calling");
                    sw.method1();
                    sw.method2();
                    sw.method3();
                    break;

            default:
                    System.out.println("No number input please!");
                    break;
        }
    }       
}
}


来源:https://stackoverflow.com/questions/14611084/multiple-characters-in-a-switch-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!