The == operator cannot be applied to java.lang.String char

I have no errors in the NetBeans Java application, but I am getting the indicated error when applying the code in the Android Java Project. I tried if (alpha[i].equals(c)) {, but then did not get any results, as in NetBeans, which converts String to Morse, for example. SOSbefore... --- ...

NetBeans Java application (works when I print SOS, I get ... ---...):

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 (doesn't work when I type a line, get nothing):

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();
}

The strange part is that this part of the code works both in NetBeans and in Android Studio (when I enter ... ---... I get 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 and morse arrays:

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 = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
    "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
    "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
    "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|"};
+4
source share
4 answers

, alpha - String, c - . (char == string), , .

+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.
+2

alpha[c]returns a string (for example, alpha[1]is the string "B"). Since it alphacontains only single-character strings, it can be converted to an array of characters:

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', ' '};
+1
source
  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++;
-2
source

All Articles