如何在 Java 中的字符串数组中查找 indexOf
How to find indexOf in a String array in Java
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 而抛出,但我不知道如何更改它。
for (int i = 0; i < input.length - 1; i++) {
if(input[i] != ' ') {
index = Arrays.asList(russian).indexOf(input[i]);
output += codeMorse[index] + " ";
}
}
您只需迭代直到 length-1
您的字符可能不在您的 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()
...
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 而抛出,但我不知道如何更改它。
for (int i = 0; i < input.length - 1; i++) {
if(input[i] != ' ') {
index = Arrays.asList(russian).indexOf(input[i]);
output += codeMorse[index] + " ";
}
}
您只需迭代直到 length-1
您的字符可能不在您的 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("Конвертировано!");
}
});