The program should turn over pieces of alphabetic data, leaving the numbers and signs in place, turns everything over, but if there are no letters at the end. Error in the condition of the cycle, but I do not know how to fix it. When correcting < to <= error pops up:

String index out of range

 import java.util.Scanner; public class Anagramm { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); //это сканнСр System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ строку"); String string = scanner.nextLine(); String first = new String(); //стСк строки ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… int stringLenght = string.length(); //Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π΄Π»ΠΈΠ½Ρ‹ строки для счСтчика Ρ†ΠΈΠΊΠ»Π° for (int i=0; i<stringLenght; i++){ char myChar = string.charAt(i); char type=simbolType(myChar); //Ρ‚ΠΈΠΏ Π°Π½Π°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠ³ΠΎ символа, отсыл ΠΊ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρƒ simbolType if (type=='l'){ first=first+myChar; } //ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½ΠΎΠΉ строки, прямой else if (type=='d') { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; int z = Character.getNumericValue(myChar); System.out.print(z); } //print digit else if (type=='w') { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; System.out.print(" "); } else { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; System.out.print(myChar); } //условиС для формирования Π±Π»ΠΎΠΊΠ° для Π°Π½Π°Π³Ρ€Π°ΠΌΠΌΡ‹ char k=type; if (k!='l'){ String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; } else {} } } public static char simbolType(char unknownSymbol) //Π±Π»ΠΎΠΊ опрСдСлСния Ρ‚ΠΈΠΏΠ° символа { char hisType; if (Character.isLetter(unknownSymbol)==true) { hisType='l'; //Ρ‚ΠΈΠΏ символа Π±ΡƒΠΊΠ²Π° } else if (Character.isDigit(unknownSymbol)==true){ hisType='d'; //Ρ‚ΠΈΠΏ символа Ρ†ΠΈΡ„Ρ€Π° } else if (Character.isWhitespace(unknownSymbol)==true){ hisType='w'; //Ρ‚ΠΈΠΏ символа ΠΏΡ€ΠΎΠ±Π΅Π» } else { hisType='a'; // Ρ‚ΠΈΠΏ символа Π΄Ρ€ΡƒΠ³ΠΎΠΉ "any" } return hisType; } } 

    2 answers 2

    In the loop, you form a string of letters that you want to draw. The conversion itself occurs only under the condition that the next character is a space or a digit. So if the line ends with letters and it comes to an end - there is no appeal (and indeed, the output of this part). To fix this, you can add a processing after the cycle with the condition that there are not displayed characters.

     if (!first.isEmpty()) { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; } 
    • Thanks for the help, Nikolai !! - JohnRoget 2:49

    I will try to write a detailed answer indicating the shortcomings and possible solutions to them.

    For starters, your code is only formatted to a more readable form. For more information, read the Google Java Style Guide .

     import java.util.Scanner; public class Anagramm { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); // это сканнСр System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ строку"); String string = scanner.nextLine(); String first = new String(); // стСк строки ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… int stringLenght = string.length(); // Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π΄Π»ΠΈΠ½Ρ‹ строки для счСтчика Ρ†ΠΈΠΊΠ»Π° for (int i = 0; i < stringLenght; i++) { char myChar = string.charAt(i); char type = simbolType(myChar); // Ρ‚ΠΈΠΏ Π°Π½Π°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠ³ΠΎ символа, отсыл ΠΊ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρƒ simbolType if (type == 'l') { first = first + myChar; // ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½ΠΎΠΉ строки, прямой } else if (type == 'd') { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; int z = Character.getNumericValue(myChar); System.out.print(z); // print digit } else if (type == 'w') { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; System.out.print(" "); } else { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; System.out.print(myChar); } // условиС для формирования Π±Π»ΠΎΠΊΠ° для Π°Π½Π°Π³Ρ€Π°ΠΌΠΌΡ‹ char k = type; if (k != 'l') { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; } else {} } } public static char simbolType(char unknownSymbol) { // Π±Π»ΠΎΠΊ опрСдСлСния Ρ‚ΠΈΠΏΠ° символа char hisType; if (Character.isLetter(unknownSymbol) == true) { hisType = 'l'; // Ρ‚ΠΈΠΏ символа Π±ΡƒΠΊΠ²Π° } else if (Character.isDigit(unknownSymbol) == true) { hisType = 'd'; // Ρ‚ΠΈΠΏ символа Ρ†ΠΈΡ„Ρ€Π° } else if (Character.isWhitespace(unknownSymbol) == true) { hisType = 'w'; // Ρ‚ΠΈΠΏ символа ΠΏΡ€ΠΎΠ±Π΅Π» } else { hisType = 'a'; // Ρ‚ΠΈΠΏ символа Π΄Ρ€ΡƒΠ³ΠΎΠΉ "any" } return hisType; } } 

    The variable stringLenght used once in the loop condition, omit its definition and calculate the length of the string directly in the loop condition.

     for (int i = 0; i < string.length(); i++) { ... } 

    Code formatting the output of the result, consisting of blocks of conditions if , else if , else , can be rewritten using the switch , so that the code will look cleaner.

     switch (type) { case 'l': first = first + myChar; // ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½ΠΎΠΉ строки, прямой break; case 'd': String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; int z = Character.getNumericValue(myChar); System.out.print(z); // print digit break; case 'w': String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; System.out.print(" "); break; default: String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; System.out.print(myChar); } 

    Some actions in the d , w , default blocks are repeated, you can optimize and combine them.

     switch (type) { case 'l': first += myChar; // ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½ΠΎΠΉ строки, прямой break; case 'd': case 'w': default: String r = new StringBuffer(first).reverse().toString(); first = ""; System.out.print(r); System.out.print(myChar); } 

    In this piece of code, nothing happens, because at the entrance to the if variable first always empty. You can safely delete it.

     char k = type; if (k != 'l') { String r = new StringBuffer(first).reverse().toString(); System.out.print(r); first = ""; } else {} 

    As a result, we received such a code.

     import java.util.Scanner; public class Anagramm { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); // это сканнСр System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ строку"); String string = scanner.nextLine(); String first = new String(); // стСк строки ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… for (int i = 0; i < string.length(); i++) { char myChar = string.charAt(i); char type = simbolType(myChar); // Ρ‚ΠΈΠΏ Π°Π½Π°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠ³ΠΎ символа, отсыл ΠΊ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρƒ simbolType switch (type) { case 'l': first += myChar; // ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½ΠΎΠΉ строки, прямой break; case 'd': case 'w': default: String r = new StringBuffer(first).reverse().toString(); first = ""; System.out.print(r); System.out.print(myChar); } } } public static char simbolType(char unknownSymbol) { // Π±Π»ΠΎΠΊ опрСдСлСния Ρ‚ΠΈΠΏΠ° символа char hisType; if (Character.isLetter(unknownSymbol) == true) { hisType = 'l'; // Ρ‚ΠΈΠΏ символа Π±ΡƒΠΊΠ²Π° } else if (Character.isDigit(unknownSymbol) == true) { hisType = 'd'; // Ρ‚ΠΈΠΏ символа Ρ†ΠΈΡ„Ρ€Π° } else if (Character.isWhitespace(unknownSymbol) == true) { hisType = 'w'; // Ρ‚ΠΈΠΏ символа ΠΏΡ€ΠΎΠ±Π΅Π» } else { hisType = 'a'; // Ρ‚ΠΈΠΏ символа Π΄Ρ€ΡƒΠ³ΠΎΠΉ "any" } return hisType; } }