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

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

问题描述

我一直在寻找适合所有年龄段,并没有发现任何人谁拥有了同样的问题我。当我在Eclipse中运行我的程序,一切都看起来很好。当我在CMD窗口中运行它,在我的ArrayList的所有特殊的非ASCII字符以取代?。有在狗类的两个属性是字符串,即名和种族。

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".

这里的code,打印在我的主程序名单:

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:

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对象:

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

当我打印出在Eclipse名单我已经添加了显示为一个Dog对象后:

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

[Bjäbbis舍费尔12 AR12.0公斤斯万人= 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?巴士转车站SCH?FER 12 AR12.0千克斯万人= 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.

我也超限的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();
}

任何帮助AP preciated!
TIA

Any help appreciated! TIA

推荐答案

编辑:我已经取消我的答案,这样它的信息现在是正确的。

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

包含特殊字符,如A,A普通字符串,ö工作的原因是因为那些都设有codeD的方式,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无法读取codeD,因此,你必须为确保所有扫描仪输入都设有codeD正确等等该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.

这是可能通过修改扫描仪设置输入的字符编码​​:

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

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

编辑:在Windows中的另一个问题产生了CMD不接受CHCP变化。
<一href=\"http://stackoverflow.com/questions/8142058/chcp-is-not-recognized-as-an-internal-or-external-command-operable-program-or\">'chcp'不被识别为内部或外部的命令,可操作的程序或批处理文件。在Windows PC

编辑:设置cmd以CHCP 65001 / UTF-8没有工作

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

结论:CMD不支持UTF-8 BYT默认,但CMD设置为UTF-8(65001 CHCP)不能用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没有显示A,A,ö字符正确的,所以我不得不Eclipse控制台手动设置为默认dispalying CHCP 850与Windows CMD一样。

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天全站免登陆