简体   繁体   English

特殊的非ASCII字符显示为? 打印ArrayList时

[英]special non-ASCII characters displayed as ? when printing ArrayList

I've been searching around for ages and haven't found anyone who has had the same problem as me. 我一直在寻找多年,并没有找到任何与我有同样问题的人。 When I run my program in Eclipse, everything looks fine. 当我在Eclipse中运行我的程序时,一切看起来都很好。 As soon as I run it in windows CMD, all special non-ASCII characters in my ArrayList are replaced with a ?. 一旦我在Windows CMD中运行它,我的ArrayList中的所有特殊非ASCII字符都被替换为?。 There are two attributes in the Dog class that are strings, namely "name" and "race". Dog类中有两个属性是字符串,即“name”和“race”。

Here's the code that prints the list in my main program: 这是在我的主程序中打印列表的代码:

System.out.println("\r\nLista på hundar i hundregistret: " + viewDogList.toString() + "\r\n");

Here's information from my Dog class, attributes, and methods used: 以下是我的Dog类,属性和方法的信息:

private String name; //attribute for the dog's name
private String race; //attribute for the dog's race

public Dog(String name, String race, int age, double weight)

        public String getName() { //hämta hundnamn      
        return name;
        }

        public void setName (String name) { //sätta hundnamn
            this.name = name;
        }

        public String getRace() {
            return race;            
        }

        public void setRace (String race) { //sätta hundras
            this.race = race;
        }

This is how the Dog list is constructed and the Dog object added: 这就是Dog列表的构造方式和Dog对象的添加方式:

ArrayList<Dog> viewDogList= new ArrayList<Dog>();
Dog dogInstance = new Dog("", "", 0, 0.0);
viewDogList.add(dogInstance);

When I print out the list in Eclipse after I've added a Dog object it is displayed as: 当我在添加Dog对象后在Eclipse中打印出列表时,它显示为:

[Bjäbbis Schäfer 12 år 12.0 kg svans=14.4] [BjäbbisSchäfer12år12.0kg svans = 14.4]

However, if I compile and run the program in CMD the same line is displayed as: 但是,如果我在CMD中编译并运行程序,则相同的行显示为:

[Bj?bbis Sch?fer 12 år 12.0 kg svans=14.4] [Bj?bbis Sch?fer12år12.0kg svans = 14.4]

Is there any solution into getting this to work? 是否有任何解决方案让这个工作? I have read something about bytes, string, character conversions but I don't think it's what I'm looking for! 我已经阅读了有关字节,字符串,字符转换的内容,但我不认为这是我正在寻找的内容!

EDIT: I forgot to mention that all strings unrelated to the ArrayList are properly displayed in the windows CMD. 编辑:我忘了提到所有与ArrayList无关的字符串都正确显示在Windows CMD中。 So its strange that only the ArrayList contents are displayed incorrectly. 所以奇怪的是只有ArrayList内容显示不正确。

I have also overrun the .toString method in the Dog class like so: 我也在Dog类中溢出了.toString方法,如下所示:

public String toString() {
    return name + " " + race + " " + getAge() + " år " + getWeight() + " kg " + "svans="+ getTailLength();
}

Any help appreciated! 任何帮助赞赏! TIA TIA

EDIT: I have revoked my answer so that its information is now correct. 编辑:我已撤销我的答案,以便其信息现在正确。

The reason normal strings containing special characters such as å, ä, ö worked was because that those are encoded in a way that cmd can read. 包含特殊字符(例如å,ä,ö)的普通字符串工作的原因是因为它们以cmd可以读取的方式编码。

when you use the scanner, the strings are encoded in a way that cmd cannot read, thus, you have to make sure all scanner inputs are encoded properly so that the cmd can read it. 当您使用扫描仪时,字符串以cmd无法读取的方式进行编码,因此,您必须确保所有扫描仪输入都已正确编码,以便cmd可以读取它。

It's possible to set the character encoding of input by modifying Scanner: 可以通过修改Scanner来设置输入的字符编码:

new Scanner(System.in, "UTF-8")

EDIT: Another problem in Windows resulted in cmd not accepting chcp changes. 编辑:Windows中的另一个问题导致cmd不接受chcp更改。 'chcp' is not recognized as an internal or external command, operable program or batch file. 'chcp'不被识别为内部或外部命令,可操作程序或批处理文件。 on a Windows PC 在Windows PC上

EDIT: Setting cmd to chcp 65001/UTF-8 did not work. 编辑:将cmd设置为chcp 65001 / UTF-8不起作用。

Conclusion: cmd does not support UTF-8 byt default, but setting cmd to UTF-8 (chcp 65001) does not work with java. 结论:cmd默认情况下不支持UTF-8,但将cmd设置为UTF-8(chcp 65001)不适用于java。 The output is still incorrect and the program crashes if you input non-ascii characters anyway. 输出仍然不正确,如果您输入非ascii字符,程序将崩溃。

EDIT: 编辑:

There is absolutely NO way to make cmd work with UTF-8. 绝对没有办法使cmd与UTF-8一起工作。 I had to the scanner to: 我不得不在扫描仪上:

new Scanner(System.in, "cp850")

Of course, this made Eclipse not showing å,ä, ö characters correct, so I had to manually set the Eclipse console to dispalying chcp 850 like the windows cmd does by default. 当然,这使得Eclipse没有显示å,ä,ö字符是正确的,所以我不得不手动将Eclipse控制台设置为与Windows cmd默认情况下的chcp 850不同。

Microsoft is at fault for all of this. 微软对所有这一切都有错。 There's absolutely no logic that cmd doesn't support UTF-8 and never has. 毫无疑问,cmd不支持UTF-8而且从来没有。 It's so stupid. 这太愚蠢了。 I bet it has to do with greedy M$ wanting $$$. 我打赌它与贪婪的M $想要的$$$有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM