我在NetBeans Java应用程序中没有出现任何错误,但在将代码应用到Android Java项目中时,我确实收到了上述错误。我尝试了if (alpha[i].equals(c)) {
,但后来我得不到像NetBeans那样的结果,它将String转换为Morse,例如SOS
至... --- ...
NetBeans Java应用程序(有效,当我输入SOS时,我得到...... ---):
private static String toMorse(String text) {
char[] characters = text.toUpperCase().toCharArray();
StringBuilder morseString = new StringBuilder();
for (char c : characters) {
for (int i = 0; i < alpha.length; i++) {
if (alpha[i] == c) {
morseString.append(morse[i] + " ");
break;
}
}
}
return morseString.toString();
}
Android Java Project(不起作用,当我输入字符串时,我什么也得不到):
public String toMorse(String text) {
char[] characters = text.toUpperCase().toCharArray();
StringBuilder morseString = new StringBuilder();
for (char c : characters) {
for (int i = 0; i < alpha.length; i++) {
if (alpha[i] == c) { // error is on this line
morseString.append(morse[i] + " ");
break;
}
}
}
return morseString.toString();
}
奇怪的是,这部分代码在NetBeans和Android Studio中都有效(当我输入...... ---我得到SOS时):
public String toEnglish(String text) {
String[] strings = text.split(" ");
StringBuilder translated = new StringBuilder();
for (String s : strings) {
for (int i = 0; i < morse.length; i++) {
if (morse[i].equals(s)) {
translated.append(alpha[i]);
break;
}
}
}
return translated.toString();
}
alpha和morse数组:
private String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", " "};
private String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|"};
答案 0 :(得分:2)
alpha
是String
(不是char
)的数组,一种可能的解决方法
private char[] alpha = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0', ' '};
另一个是
if (alpha[i].charAt(0) == c) { // <-- a String is not a char.
答案 1 :(得分:2)
问题是alpha
是String
的数组,而c
是一个字符。
你正在比较(char == string)
,这显然不像预期那样有效。
答案 2 :(得分:1)
alpha[c]
返回一个字符串(例如alpha[1]
是字符串&#34; B&#34;)。
由于alpha
仅包含单字符字符串,因此可以将其转换为字符数组:
private char[] alpha = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0', ' '};
答案 3 :(得分:-1)
char[] alpha1 = new char[26] {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z'
};
char[] cipher = new char[26] {
'Z',
'Y',
'X',
'W',
'V',
'U',
'T',
'S',
'R',
'Q',
'P',
'O',
'N',
'M',
'L',
'K',
'J',
'I',
'H',
'G',
'F',
'E',
'D',
'C',
'B',
'A'
};
string plain = textBox1.Text;
char[] N = textBox1.Text.ToArray();
textBox3.Text = "";
int X = 0;
for (int i = 0; i < alpha1.Length; i++) {
if (N[0] == alpha1[0])
textBox3.Text += cipher[X];
X++;
}
//..............DISPLAY
textBox3.Text = "";
int b = 0;
for (int i = 0; i < cipher.Length; i++) {
textBox3.Text += cipher[b];
}
b++;