如何在Java中的String数组中找到indexOf

时间:2017-08-25 13:25:48

标签: java arrays string indexof

button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String output = "";
            char[] input = textArea.getText().toUpperCase().toCharArray();
            int index;

            for (int i = 0; i < input.length; i++) {
                if(input[i] != ' ') {
                    index = Arrays.asList(russian).indexOf(input[i]);
                    output += codeMorse[index] + " ";
                }
            }

            textArea.setText(output);
            button.setText("Конвертировано!");
        }
    });

//阵列

    char[] russian = new char[]{'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И',
                            'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С',
                            'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ы', 'Ь',//41
                            'Э', 'Ю', 'Я', '1', '2', '3', '4', '5', '6', '7',
                            '8', '9', '0' };

String[] codeMorse = new String[] { "*–", "–***", "*––", "––*",
                                    "–**", "*", "***–", "––**",
                                    "**", "*–––", "–*–", "*–**", //41
                                    "––", "–*", "–––", "*––*",
                                    "*–*", "***", "–", "**–",
                                    "**–*", "****", "–*–*",
                                    "–––*", "––––", "−−*−",
                                    "−*−−", "−**−", "**−**",
                                    "**−−", "*−*−", "*−−−−",
                                    "**−−−", "***−−", "****−",
                                    "*****", "−****", "−−***",
                                    "−−−**", "−−−−*", "−−−−−" };

//输入值为“привет”

此代码抛出java.lang.ArrayIndexOutOfBoundsException:-1异常。两个阵列的长度相同。我认为它会因indexOf而抛出,但我不知道如何改变它。

2 个答案:

答案 0 :(得分:0)

您的字符可能不在russian数组中。

您使用的toUpperCase()未指定Locale。请为Locale指定new Locale("ru"),因为它可能会错误地转换您的小写字母。

非字母数字字符也会导致问题。

尝试这样的事情:

private static final Locale RUSSIAN = new Locale("ru");

private static final String russian = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЬ" + // 41
                                      "ЭЮЯ1234567890";

button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String output = "";
            char[] input = textArea.getText().toUpperCase(RUSSIAN).toCharArray();
            int index;

            for (int i = 0; i < input.length; i++) {
                index = russian.indexOf(input[i]);
                System.out.printf("char: %c, index: %d%n", input[i], index);
                if (index >= 0)
                    output += codeMorse[index] + " ";
            }

            textArea.setText(output);
            button.setText("Конвертировано!");
        }
    });

char[]转换为String以删除Arrays.asList() ...

答案 1 :(得分:-3)

for (int i = 0; i < input.length - 1; i++) {
                if(input[i] != ' ') {
                    index = Arrays.asList(russian).indexOf(input[i]);
                    output += codeMorse[index] + " ";
                }
            }

你只需要迭代到length-1

相关问题