There is a program-catalog of drinks. Why the data entered by the user, namely, the name and type on the console are displayed with all sorts of characters? And how to fix it?

import java.util.*; import java.util.ArrayList; import java.util.Scanner; public class Katalog { static ArrayList<String> name = new ArrayList<String>(); static ArrayList<String> type = new ArrayList<String>(); static ArrayList<Double> percent = new ArrayList<Double>(); static Scanner in = new Scanner(System.in); public static void main(String[] args) { while(true) { System.out.println("Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ дСйствиС: "); System.out.println("1 - ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ вСсь ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³"); System.out.println("2 - Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π½ΠΎΠ²Ρ‹ΠΉ Π½Π°ΠΏΠΈΡ‚ΠΎΠΊ Π² ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³"); System.out.println("3 - поиск Π½Π°ΠΏΠΈΡ‚ΠΊΠ° с минимальной Ρ†Π΅Π½ΠΎΠΉ ΠΏΡ€ΠΈ крСпости Π½Π΅ Π½ΠΈΠΆΠ΅ Π·Π°Π΄Π°Π½Π½ΠΎΠΉ"); int choose = in.nextInt(); switch(choose) { case 1: printall(); break; case 2: add(); break; case 3: found_minimum(); break; } System.out.println(""); } } static void printall() { for(int i=0;i<name.size();i++) System.out.println(printone(i)); } static String printone(int i) { return "НаимСнованиС: " + name.get(i)+ " - Π’ΠΈΠΏ " + type.get(i) +" - Π‘ΠΏΠΈΡ€Ρ‚Π° " + percent.get(i)+ "%."; } static void add() { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π½Π°ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½ΠΈΠ΅: "); name.add(in.next()); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ‚ΠΈΠΏ: "); type.add(in.next()); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΡ†Π΅Π½Ρ‚Π½ΠΎΠ΅ содСрТаниС спирта: "); percent.add(in.nextDouble()); } static void found_minimum() { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π½ΠΈΠΆΠ½ΠΈΠΉ ΠΏΡ€Π΅Π΄Π΅Π» ΠΏΡ€ΠΎΡ†Π΅Π½Ρ‚Π° содСрТания спирта: "); double limit = in.nextDouble(); Boolean found = false; if(percent.size()>1) { int pos = 0; double max = percent.get(0); for(int i=0;i<percent.size();i++) if(percent.get(i)>max){ max=percent.get(i);pos=i;} double min = max; if(min>=limit) { found = true; for(int i=0;i<percent.size();i++) if(percent.get(i)<min && percent.get(i)>=limit){ min=percent.get(i);pos=i;} } if(found) System.out.println("ΠœΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΠΎ ΠΏΡ€ΠΎΡ†Π΅Π½Ρ‚Ρƒ содСрТания спирта: " + printone(pos)); else System.out.println("Π’Π°ΠΊΠΎΠ³ΠΎ Π½Π°ΠΏΠΈΡ‚ΠΊΠ° Π½Π΅Ρ‚!"); } } } 

Reported as a duplicate by Mikhail Vaysman , Denis , aleksandr barakin , Pavel Mayorov , Denis Bubnov on Jun 15 '17 at 6:26 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Does the program run on Windows? - Mikhail Vaysman
  • @MikhailVaysman, on Windows - mark79

1 answer 1

The internal representation of strings in java is unicode. The encoding of the lines in the source code is most likely utf-8. Windows console encoding is cp866. In the process of concatenating strings with different encoding, the cracks are obtained. This trouble can be backed up with a crutch:

 public class Katalog { static ArrayList<String> name = new ArrayList<String>(); static ArrayList<String> type = new ArrayList<String>(); static ArrayList<Double> percent = new ArrayList<Double>(); static Scanner in; public static void main(String[] args) { String os = System.getProperty("os.name"); String consoleEncoding = System.getProperty("console.encoding", os.startsWith("Windows") ? "cp866" : "utf-8"); in = new Scanner(System.in, consoleEncoding); while(true) { System.out.println("Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ дСйствиС: "); System.out.println("1 - ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ вСсь ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³"); System.out.println("2 - Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π½ΠΎΠ²Ρ‹ΠΉ Π½Π°ΠΏΠΈΡ‚ΠΎΠΊ Π² ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³"); System.out.println("3 - поиск Π½Π°ΠΏΠΈΡ‚ΠΊΠ° с минимальной Ρ†Π΅Π½ΠΎΠΉ ΠΏΡ€ΠΈ крСпости Π½Π΅ Π½ΠΈΠΆΠ΅ Π·Π°Π΄Π°Π½Π½ΠΎΠΉ"); System.out.println("4 - Π²Ρ‹Ρ…ΠΎΠ΄"); int choose = in.nextInt(); switch(choose) { case 1: printall(); break; case 2: add(); break; case 3: found_minimum(); break; case 4: return; } System.out.println(""); } } // ΠžΡΡ‚Π°Π»ΡŒΠ½ΠΎΠ΅ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ } 

In Windows, strings will be read in cp866 encoding, in other operating systems in utf-8. If necessary, you can override this autodetection with the -Dconsole.encoding key when starting the program.