简体   繁体   中英

Constant Expression Required When Using an Array With a Switch Statement

Hello StackOverflow community, I've just begun to work with arrays and I was wondering how I can make the switch statement work with the values below. It's really irritating that it won't work considering the only error is constant expression required:

import java.lang.Character;

public class Ass11a {

    public static char[] vowel = new char[4];
    public static char[] consonant = new char[20];
    public static char[] number = new char[9];
    public static char[] punctuation = new char[10];
    public static void main(String[] args) {
        EasyReader console = new EasyReader();
        System.out.print("Enter a character: ");
        char input = console.readChar();
        Character.toLowerCase(input);
        vowel[0] = 'a';
        vowel[1] = 'e';
        vowel[2] = 'i';
        vowel[3] = 'o';
        vowel[4] = 'u';

        consonant[0] = 'q';
        consonant[1] = 'w';
        consonant[2] = 'r';
        consonant[3] = 't';
        consonant[4] = 'y';
        consonant[5] = 'p';
        consonant[6] = 's';
        consonant[7] = 'd';
        consonant[8] = 'f';
        consonant[9] = 'g';
        consonant[10] = 'h';
        consonant[11] = 'j';
        consonant[12] = 'k';
        consonant[13] = 'l';
        consonant[14] = 'z';
        consonant[15] = 'x';
        consonant[16] = 'c';
        consonant[17] = 'v';
        consonant[18] = 'b';
        consonant[19] = 'n';
        consonant[20] = 'm';

        number[0] = '1';
        number[1] = '2';
        number[2] = '3';
        number[3] = '4';
        number[4] = '5';
        number[5] = '6';
        number[6] = '7';
        number[7] = '8';
        number[8] = '9';
        number[9] = '0';

        punctuation[0] = '.';
        punctuation[1] = ',';
        punctuation[2] = '?';
        punctuation[3] = '!';
        punctuation[4] = ';';
        punctuation[5] = ':';
        punctuation[6] = '"';
        punctuation[7] = '\'';
        punctuation[8] = '(';
        punctuation[9] = ')';
        punctuation[10] = '-';                                                                                                                                               
        switch(input)
        {
            case vowel[0]:case vowel[1]:case vowel[2]:case vowel[3]:case vowel[4]:
                System.out.println("Vowel");
                break;
            case consonant[0]:case consonant[1]:case consonant[2]:case consonant[3]:
            case consonant[4]:case consonant[5]:case consonant[6]:case consonant[7]:
            case consonant[8]:case consonant[9]:case consonant[10]:case consonant[11]:
            case consonant[12]:case consonant[13]:case consonant[14]:case consonant[15]:
            case consonant[16]:case consonant[17]:case consonant[18]:case consonant[19]:
            case consonant[20]:
                System.out.println("Consonant");
                break;
            case number[0]:case number[1]:case number[2]:case number[3]:case number[4]:
            case number[5]:case number[6]:case number[7]:case number[8]:case number[9]: 
                System.out.println("Number"); 
                break;                              
            case punctuation[0]:case punctuation[1]:case punctuation[2]:case punctuation[3]:
            case punctuation[4]:case punctuation[5]:case punctuation[6]:case punctuation[7]:
            case punctuation[8]:case punctuation[9]:case punctuation[10]:       
                System.out.println("Punctuation");
                break;
            default:
                System.out.println("Other"); 
                break;              
        }
    }
}

You could use an enum like this

public enum Vowels {
  A,E,I,O,U;
  static boolean isVowel(char c) {
    switch (c) {
    case 'A': case 'a':
    case 'E': case 'e':
    case 'I': case 'i':
    case 'O': case 'o':
    case 'U': case 'u': return true;
    }
    return false;
  }
  static Vowels getVowel(char c) {
    switch (c) {
    case 'A': case 'a': return A;
    case 'E': case 'e': return E;
    case 'I': case 'i': return I;
    case 'O': case 'o': return O;
    case 'U': case 'u': return U;
    }
    return null;
  }
}

and use it like

public static void main(String[] args) {
  Scanner console = new Scanner(System.in);
  while (console.hasNextLine()) {
    String input = console.nextLine();
    for (Character ch : input.toCharArray()) {
      if (Vowels.isVowel(ch)) {
        System.out.println(ch + " is vowel");
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM