user1906927
user1906927

Reputation: 21

Making cyrillic readable for the Scanner Object in Java

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

Upvotes: 2

Views: 2641

Answers (1)

dim
dim

Reputation: 1032

(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.

Upvotes: 2

Related Questions