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