gooooodmorning
gooooodmorning

Reputation: 5

Make switch case detect characters and input them into an array

Not sure if the switch is what's causing the problem, but this switch is supposed to convert Latin characters into its Cyrillic "form". It seems to go to the default switch every time.

Also unsure whether inputting the parameter as a String.toCharArray() causes problems

public static void main(String[] args) {
cyr("abcekmh".toLowerCase().toCharArray());
}
private static void cyr(char[] c) {
           char[] d = new char[c.length];
           for(int i = 0; i < c.length; i++) {
               switch((char)c[i]) {
               case 'a':
                   d[i] = 'а';
               case 'b':
                   d[i] = 'в';
               case 'e':
                   d[i] = 'е';
               case 'k':
                   d[i] = 'к';
               case 'm':
                   d[i] = 'м';
               case 'h':
                   d[i] = 'н';
               case 'i':
                   d[i] = 'ı';
               case 'o':
                   d[i] = 'о';
               case 'p':
                   d[i] = 'р';
               case 'c':
                   d[i] = 'с';
               case 't':
                   d[i] = 'т';
               case 'y':
                   d[i] = 'у';
               case 'x':
                   d[i] = 'х';
               default:
                   d[i] = c[i];
               }
           }
           for(char russian : d) {
               System.out.print(russian);
           }
       }

Upvotes: 0

Views: 244

Answers (1)

BetaRide
BetaRide

Reputation: 16844

Your code is missing the break statement at the end of a case block. Without break you get a fall through to the next case/default.

See The switch Statement documentation.

Upvotes: 2

Related Questions