java编码问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 16:46:57
编写一个java程序,打印对应十进制整数15000~20000之间的Unicode编码的字符。

public class Unicode2Char {

public static void main(String[] args) {
System.out.println(getChar(getUnicode()));
}
static String getUnicode(){
System.out.println("Please enter an integer :");
Scanner in=new Scanner(System.in);
String s = "";
int i=0;
try {
i=in.nextInt();
} catch (Exception e) {
System.out.println("Invalid input.");
}
s="\\u"+Integer.toHexString(i);
return s;
}
static String getChar(String s){
Pattern p = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher m = p.matcher(s);
char c;
while (m.find()) {
c = (char) Integer.parseInt(m.group(2), 16);
s = s.replace(m.group(1), c + "");
}
return s;
}
}