特殊的非 ASCII 字符显示为 ?打印 ArrayList 时 [英] special non-ASCII characters displayed as ? when printing ArrayList

查看:44
本文介绍了特殊的非 ASCII 字符显示为 ?打印 ArrayList 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找,但没有找到和我有同样问题的人.当我在 Eclipse 中运行我的程序时,一切看起来都很好.一旦我在 Windows CMD 中运行它,我的 ArrayList 中的所有特殊非 ASCII 字符都将替换为 ?.Dog 类中有两个属性是字符串,分别是name"和race".

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. As soon as I run it in windows CMD, all special non-ASCII characters in my ArrayList are replaced with a ?. There are two attributes in the Dog class that are strings, namely "name" and "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");

以下是我使用的 Dog 类、属性和方法的信息:

Here's information from my Dog class, attributes, and methods used:

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

Dog 列表的构建方式和 Dog 对象的添加方式如下:

This is how the Dog list is constructed and the Dog object added:

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

当我在添加 Dog 对象后在 Eclipse 中打印出列表时,它显示为:

When I print out the list in Eclipse after I've added a Dog object it is displayed as:

[Bjäbbis Schäfer 12 år 12.0 kg svans=14.4]

[Bjäbbis Schäfer 12 år 12.0 kg svans=14.4]

但是,如果我在 CMD 中编译并运行该程序,则会显示相同的行:

However, if I compile and run the program in CMD the same line is displayed as:

[Bj?bbis Sch?fer 12 år 12.0 kg svans=14.4]

[Bj?bbis Sch?fer 12 år 12.0 kg 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!

我忘了提到所有与 ArrayList 无关的字符串都正确显示在 Windows CMD 中.所以奇怪的是只有ArrayList的内容显示不正确.

I forgot to mention that all strings unrelated to the ArrayList are properly displayed in the windows CMD. So its strange that only the ArrayList contents are displayed incorrectly.

我也像这样在 Dog 类中溢出了 .toString 方法:

I have also overrun the .toString method in the Dog class like so:

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

任何帮助表示赞赏!TIA

Any help appreciated! TIA

推荐答案

我已经撤销了我的答案,现在它的信息是正确的.

I have revoked my answer so that its information is now correct.

包含 å、ä、ö 等特殊字符的普通字符串之所以有效,是因为它们以 cmd 可以读取的方式进行编码.

The reason normal strings containing special characters such as å, ä, ö worked was because that those are encoded in a way that cmd can read.

当您使用扫描仪时,字符串的编码方式是 cmd 无法读取的,因此,您必须确保所有扫描仪输入都正确编码,以便 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.

可以通过修改Scanner来设置输入的字符编码:

It's possible to set the character encoding of input by modifying Scanner:

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

Windows 中的另一个问题导致 cmd 不接受 chcp 更改.无法识别chcp"作为内部或外部命令、可运行的程序或批处理文件.在 Windows PC 上

Another problem in Windows resulted in cmd not accepting chcp changes. 'chcp' is not recognized as an internal or external command, operable program or batch file. on a Windows PC

将 cmd 设置为 chcp 65001/UTF-8 不起作用.

Setting cmd to chcp 65001/UTF-8 did not work.

结论:cmd默认不支持UTF-8,但是设置cmd为UTF-8(chcp 65001)对java不起作用.输出仍然不正确,如果您无论如何输入非 ASCII 字符,程序就会崩溃.

Conclusion: cmd does not support UTF-8 byt default, but setting cmd to UTF-8 (chcp 65001) does not work with java. The output is still incorrect and the program crashes if you input non-ascii characters anyway.

绝对没有办法让 cmd 与 UTF-8 一起工作.我不得不使用扫描仪:

There is absolutely NO way to make cmd work with UTF-8. I had to the scanner to:

new Scanner(System.in, "cp850")

当然,这使 Eclipse 无法正确显示 å,ä, ö 字符,因此我必须像 windows cmd 默认情况下一样手动将 Eclipse 控制台设置为显示 chcp 850.

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.

这一切都是微软的错.绝对没有 cmd 不支持 UTF-8 并且从来没有的逻辑.这太愚蠢了.我敢打赌这与贪婪的 M$ 想要 $$$ 有关.

Microsoft is at fault for all of this. There's absolutely no logic that cmd doesn't support UTF-8 and never has. It's so stupid. I bet it has to do with greedy M$ wanting $$$.

这篇关于特殊的非 ASCII 字符显示为 ?打印 ArrayList 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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