Java Eclipse控制台不打印汉字
我已经编写了一个Java函数,它接受一个字符串参数,并使用一些逻辑从中生成一个随机id。如果我的字符串包含英文字符,一切正常,但当我传递中文字符时,这些字符被替换为 这是我的密码:Java Eclipse控制台不打印汉字,java,eclipse,utf-8,Java,Eclipse,Utf 8,我已经编写了一个Java函数,它接受一个字符串参数,并使用一些逻辑从中生成一个随机id。如果我的字符串包含英文字符,一切正常,但当我传递中文字符时,这些字符被替换为 这是我的密码: public static String generateId(String inputString) { /** * Split input string on the basis of white spaces */ String arr[] =
public static String generateId(String inputString) {
/**
* Split input string on the basis of white spaces
*/
String arr[] = inputString.split(" ");
/**
* Change the first character of first substring to lowercase
*/
String id = arr[0].substring(0, 1).toLowerCase() + arr[0].substring(1);
if(arr.length > 1)
{
for (int i = 1; i < arr.length; i++) {
/**
* Change the first character of remaining substrings to uppercase
* and append to id
*/
if(arr[i].trim().length() != 0)
{
id = id + arr[i].substring(0, 1).toUpperCase() + arr[i].substring(1);
}
}
}
int length = id.length();
Random random = new Random();
/**
* If the length of id is less than 8 then append random digits to make
* length equals to 8 else take a substring of length equals to 8
*/
if (length < 8) {
int len = 8 - length;
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
sb.append((char) ('0' + random.nextInt(10)));
}
id = id + sb;
} else {
id = id.substring(0, 8);
}
/**
* Append 4 digits random number to make length of id equals to 12
* characters long
*/
return id + String.format("%04d", random.nextInt(10000));
}
输出:anyid0660920
System.out.println(MyClass.generateId("这是标题"));
输出:14102367
如何处理此问题?将控制台编码更改为
UTF-8
转到Run->Run Configurations->Common选项卡->控制台编码(或在较新版本中仅编码)->选择UTF-8
默认情况下,它是不支持中文的拉丁编码(8859) 将控制台编码更改为
UTF-8
转到Run->Run Configurations->Common选项卡->控制台编码(或在较新版本中仅编码)->选择UTF-8
默认情况下,它是不支持中文的拉丁编码(8859) 它工作得很好,我假设您的控制台没有配置为使用UTF-8编码。输出为:
这是标题71066391
对于4个Unicode字符,您将获得4个?
。我假设您的默认字符集无法显示它们,它们被问号替换。更进一步,您应该给出操作系统、版本和显示类型(GUI、终端、Windows控制台)及其字符集配置。它工作正常,我假设您的控制台没有配置为使用UTF-8编码。输出为:这是标题71066391
对于4个Unicode字符,您将获得4个?
。我假设您的默认字符集无法显示它们,它们被问号替换。更进一步,您应该给出操作系统、版本和显示类型(GUI、终端、Windows控制台)及其字符集配置
System.out.println(MyClass.generateId("这是标题"));