java新手,TX们帮看看以下代码问题出在哪里。

来源:百度知道 编辑:UC知道 时间:2024/05/21 16:31:45
public class str6
{
public static void main(String[] args)
{
byte[] b={1,2,3,4,5,6};
String str=new String(b,3,2,UTF-16);
System.out.println(str);
}
}
=================无奈的分割线=============================
编译后出现以下信息:
str6.java:6: 找不到符号
符号: 变量 UTF
位置: 类 str6
String str=new String(b,3,2,UTF-16);
^
注意:str6.java 使用或覆盖了已过时的 API。
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
1 错误

=============================================================================
从一楼到六楼说的方式我都一一试过,好像都不行啊。是不是和我的版本有关啊,我的JAVA版本是1.6.0_15


String str=new String(b,3,2,UTF-16);

修改为

public class str6
{
public static void main(String[] args)
{
byte[] b={1,2,3,4,5,6};
String str=new String(b,3,2,"UTF-16");
System.out.println(str);
}
}
应该就可以了!

针对于楼主的补充回答:
首先修改成
public class Str6 {
public static void main(String[] args){
byte[] b={1,2,3,4,5,6};
String str;
try {
str = new String(b,3,2,"UTF-16");
System.out.println(str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

但是显示的时候为
?
不知道楼主的运行结构是什么?如果说楼主按上面的代码修改后运行时报错
就有可能是jdk版本的问题
我用的是1.5

一楼正解

捕捉一下转码的异常

byte[] b={1,2,3,4,5,6};
String str;
try {
str = new String(b,3,2,"UTF-16");
System.out.println(str);
} catch (Unsupport