public class NewClass {public static void main(String[] args){byte[] b={1,2,3};String str = new String(b,2,3,UTF-16);System.out.println(str);}}//sdk java1.6 , 為什麼error//Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 找不到符號符號: 變量 UTF
2 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
先加
import java.nio.charset.Charset;
String str = new String(b,2,3,UTF-16);
改為
String str = new String(b,2,3,Charset.forName("UTF-16"));
這個地方需要的是Charset類...

夢里花落0921
TA貢獻1772條經驗 獲得超6個贊
public class NewClass {
public static void main(String[] args)
{
byte[] b={1,2,3};
String str = new String(b,2,3,UTF-16) // 這里會下標越界;
System.out.println(str);
}
}
把這段代碼改成:
import java.io.*;
public class NewClass {
public static void main(String[] args){
try{
byte[] b={1,2,3};
String str = new String(b,1,2,"UTF-16");// 這里會拋出一個i里的異常
System.out.println(str);
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
}
}
添加回答
舉報
0/150
提交
取消