When translating an array into a string

String str3="20150"; byte[] b3=str3.getBytes(); System.out.println(b3); //Массив байтов переводим обратно в строку try{ String s=new String(b3,"[B@15db9742"); System.out.println(s); }catch (UnsupportedEncodingException e){ e.printStackTrace(); 

message is issued

Exception in thread "java.lang.Error: Unresolved compilation problem: UnsupportedEncodingException cannot be resolved to a type

How to fix this problem?

  • [B@15db9742 strange argument. Try Charset.defaultCharset() instead - Herrgott
  • 2
    Specifically, a compilation error is treated by importing the java.io.UnsupportedEncodingException class. But you are doing something strange, starting from array output ( toString() on arrays displays such nonsense (type @ hashcode instance) as you do, so you should call Arrays.toString(b3) to get a human readable string). Also, getBytes() is used to get the byte representation of the string using the specified encoding (or the default encoding, as you have). In general, it would be better if you tell why you wrote it all. - zRrr
  • @zRrr Please post your comment as a response. - Nicolas Chabanovsky
  • @zRrr_ "In general, it would be better if you tell me why you wrote all this." _ This is the code from the site study-java.ru (Lesson 7). - Michael

1 answer 1

Code itself

  try{ String s=new String(b3,"UTF-8"); System.out.println(s); }catch (UnsupportedEncodingException e){ e.printStackTrace(); } 

if helped, then very happy

  • In general, it would be better if you tell why you wrote it all. This is code from study-java.ru (Lesson 7). - Michael
  • in the sense of why? I did not check the person for plagiarism. He got an error, I fixed it. It makes no difference where a person gets this code. - Dmitriy