4

Code:

Scanner sc = new Scanner(System.in);
System.out.println("Enter Name : ");
String name = sc.nextLine();
System.out.println(name);

String encoding = "UTF-8";
System.out.println(new String(name.getBytes(encoding), "euc-jp"));
System.out.println(new String(name.getBytes(encoding), "Shift_JIS"));
System.out.println(new String(name.getBytes(encoding), "ISO-2022-JP"));
System.out.println(new String(name.getBytes(encoding), "ISO8859-1"));

Input:

Enter Name : たなかです

Output:

�F�Q���N�@

鐃�鐃�鐃緒申鐃�鐃�

�ソスF�ソスQ�ソス�ソス�ソスN�ソス@

���F���Q���������N���@

�F�Q���N�@

None of them are readable Japanese. I've also tried InputStreamReader and DataInputStream with Byte[].

Community
  • 1
  • 1
  • 1
    Possible duplicate of [how to use chinese and japanese character as string in java?](http://stackoverflow.com/questions/4067628/how-to-use-chinese-and-japanese-character-as-string-in-java) – sinclair Mar 28 '16 at 11:08
  • 3
    `System.out.println(name)` should work fine - the other lines don't make much sense. The problem is probably that your console can't print those characters. See [demo](http://ideone.com/iDs0t7). – assylias Mar 28 '16 at 11:11
  • can you show us how are using "encoding" variable bdw? – Sasi Kathimanda Mar 28 '16 at 11:13
  • encoding should be = "UTF-8", I have tried to compile with encoding and running in console. both doesn't work – TanakaSakana Mar 28 '16 at 11:18
  • Instead of reading in an input, does printing a hardcoded string of Japanese characters work? E.g. `System.out.println("頑張ります!");` @TanakaSakana – Tacocat Mar 28 '16 at 11:32
  • println works, it prints out as it is – TanakaSakana Mar 28 '16 at 11:39
  • I think it should be problem of inputstream , however , new scanner(System.in, "UTF-8") not works – TanakaSakana Mar 29 '16 at 00:22
  • Can you post what the output should look like so that we can compare? – user1803551 Jul 24 '16 at 19:07

2 Answers2

0

How to print the string properly to console with your code

name.getBytes(encoding) in your code will get the raw-byte representation of the String name with UTF-8 encoding. So when you type "たなかです" in console, you will get the array of byte {0xE3, 0x81, 0x9F, 0xE3, 0x81, 0xAA, 0xE3, 0x81, 0x8B, 0xE3, 0x81, 0xA7, 0xE3, 0x81, 0x99}.

It is UTF-8 based representation, so the only encoding you can specify in the 2nd argument of the constructor String(byte[] bytes, String charsetName) is UTF-8.

System.out.println(new String(name.getBytes(encoding), "UTF-8"));

It converts the byte array {0xE3, 0x81, 0x9F, ... } to a String object, and prints to the console properly.

How to get the internal representation of a String as a byte array

String object uses UTF-16 for the internal text representation (see https://docs.oracle.com/javase/8/docs/technotes/guides/intl/overview.html for details).

So you have to use name.getBytes("UTF-16") when you want to get the byte array that same as the internal text representation. You can reverse it to a String object with System.out.println(new String(name.getBytes("UTF-16"), "UTF-16"));.

SATO Yusuke
  • 1,600
  • 15
  • 39
-1

there is slight problem in your following code snippet, you are using same encoding for different charsets,

String encoding = System.getProperty("file.encoding"); 
System.out.println(new String(name.getBytes(encoding), "UTF-8"));

assuming you want to print the japanese characters using different charset's ,use this

 System.out.println(new String(name.getBytes("euc-jp"), "euc-jp"));
 System.out.println(new String(name.getBytes("Shift_JIS"), "Shift_JIS"));
 System.out.println(new String(name.getBytes("ISO-2022-JP"), "ISO-2022-JP"));
 System.out.println(new String(name.getBytes("ISO8859-1"), "ISO8859-1"));
Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47