Read Japanese Character using Scanner

后端 未结 3 1903
醉梦人生
醉梦人生 2021-01-15 01:41

Possible duplicate: How can I read Chinese characters correctly using Scanner in Java?

My input file name may have japanese characters and I am trying to read the fi

相关标签:
3条回答
  • 2021-01-15 02:03

    Eclipse is reading from the Console which is set by default as UTF-8. To read the data you need to change the console encoding to Japanese supported encoding.

    In the Run Configuration, change the encoding to Japanese supported encoding and try again.

    enter image description here

    0 讨论(0)
  • Your code is correct, only issue is you are reading utf-8 and then converting it to Shift_JIS which prints junk characters.

    Have you tried using this.

    Scanner sc = new Scanner(System.in,"utf-8");
    System.out.println("Encoding is :" + Charset.defaultCharset());
    
    System.out.println("Enter the path:");
    String inputFilePath = sc.nextLine();
    System.out.println("Input path:" + new String(inputFilePath.getBytes("utf-8")));
    

    Hope this helps !!

    0 讨论(0)
  • 2021-01-15 02:29

    This Answer for Chinese

    Every String is already (conceptually) a sequence of characters, including Chinese characters.. Encoding only comes into it when you need to convert it into a bytes, which you don't need to for your assignment. Just use the String's hashcode. In fact, when you create a HashMap, that's exactly what will happen behind the scene

    0 讨论(0)
提交回复
热议问题