Eclipse控制台不打印汉字 [英] Eclipse console not printing Chinese characters

查看:99
本文介绍了Eclipse控制台不打印汉字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Java函数,该函数带有一个字符串参数,并使用某种逻辑从中生成一个随机id.如果我的String包含英文字符,那么一切正常,但是当我传递中文字符时,将它们替换为???

I have written a Java function which take a string parameter and generate a random id from it using some logic. Everything is working fine if my String contains English characters but when I pass Chinese characters, these are replaced by ???

这是我的代码:

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));
    }

这是我针对不同情况的输出:

Here are my outputs for different cases:

 System.out.println(MyClass.generateId("anyid"));

输出:anyid0660920

Output: anyid0660920

System.out.println(MyClass.generateId("这是标题"));

输出:???? 14102367

Output: ????14102367

我该如何处理这个问题?

how can I deal with this issue?

推荐答案

将控制台编码更改为 UTF-8

转到 Run->运行配置->通用标签->控制台编码(或仅在较新版本中为编码)->选择UTF-8 .

默认情况下,它是不支持中文的拉丁编码(8859).

By default it'd be Latin encoding (8859) which doesn't support Chinese.

这篇关于Eclipse控制台不打印汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆