Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making cyrillic readable for the Scanner Object in Java

Tags:

java

encoding

My issue is that when I input cyrillic letters into the Scanner, that when I try to print it out it becomes gobbldygook (Eg. input ходить, output = Ö–æ–¥–∏). I have the Ascii values of the cyrillic alphabet as well as the UTF-8 values stored in a text file. I am pretty certain that System.in is wrong, so what exactly should I be doing?

Scanner s = new Scanner(System.in);
String line = s.nextLine();
System.out.println(line);
like image 746
user1906927 Avatar asked Jan 22 '26 12:01

user1906927


1 Answers

(I apologize in advance for my english) I had the same problem. I working with "eclipse" and text file encoding is UTF-8. When I input cyrillic text from the console and when I try to print it the output result is similar like yours.

Scanner input = new Scanner(System.in,  "UTF-8");
      String word = input.nextLine();
      System.out.println(word);

result:

дума
РґСѓРјР°

The two lines must be same but they aren'n.

My resolve is:

Scanner input = new Scanner(System.in,  "UTF-8");
      String word = input.nextLine();

      try {
        word = new String(word.getBytes("windows-1251"), Charset.forName("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      System.out.println(word);

result:

дума
дума

This is the right result.

I'm sorry but my english is very poor... I hope I helped you.

like image 174
dim Avatar answered Jan 24 '26 02:01

dim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!