public class Program { public static void main(String[] args) { StringBuilder alphabet = new StringBuilder("абвгдеёжзийклмнопрстуфхцчшщъыьэюя "); String text= "привет меня зовут антон я программист Java"; StringBuilder textResult = new StringBuilder(text); for(int i =0; i< text.length(); i++) { textResult.insert(i, alphabet.indexOf(____text[i]____ // Здесь выдает ошибку Array type expected; found java.lang.String)); } System.out.print(textResult); } } 

In the code I wrote where this error is and what it writes. The essence of the program is to change the letters in the text to their serial number in the alphabet.

    2 answers 2

    StringBuilder does not make sense here, use a regular string. You can also use the substring method to get the i-th character of the text string:

     String alphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя "; String text= "привет меня зовут антон я программист Java"; StringBuilder textResult = new StringBuilder(text); for(int i = 0; i < text.length(); i++) { textResult.insert(i, alphabet.indexOf(text.substring(i, i+1))); } System.out.println(textResult); 

    Test for ideone .

      you have a text like String - this is not an array !!! You can do something like this:

        public class Program { public static void main(String[] args) { StringBuilder alphabet = new StringBuilder("abcdef"); String text= "abc def adddd ef"; String text1 = text.replaceAll(" ", ""); char[] ch = text1.toCharArray(); StringBuilder textResult = new StringBuilder(); for(int i = 0; i < ch.length; i++) { textResult.append(alphabet.indexOf(String.valueOf(ch[i])) + 1); } System.out.print(textResult); } } 
      • 2
        The array has no length () method, only the property and the builder does not have the indexOf method for indexOf(char) - Aleksey Shimansky