我想写一个希腊语词汇测验程序。问题是我无法正确解释输入字符。以下是一些示例代码,我将它们放在一起来展示问题。 (如果你不想经历为你的机器设置希腊文输入的麻烦,当程序要求输入这个单词时,你可以复制并粘贴希腊字符串。如果它很重要,我可以通过日食在64位的Win7)如何从键盘上读取Unicode希腊字母?
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class GreekKeyboardExample {
public static void main(String[] args) {
String word = "αβγδεζηθικλμνξοπρσςτυφχψω";
System.out.println("\n\n" + word + "\n");
String answer = getInput("Type the word above: ");
System.out.println("\nThis is what the computer took from the keyboard:");
printCharsAndCode(answer);
System.out.println("\nThis is what it should look like:");
printCharsAndCode(word);
}
private static String getInput(String prompt) {
System.out.print(prompt);
System.out.flush();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF8"));
return in.readLine();
}
catch (Exception e) {
return "Error: " + e.getMessage();
}
}
/* prints the character and its (unicode) code */
public static void printCharsAndCode(String str) {
// int len = str.length();
char[] c = str.toCharArray();
System.out.println(str);
for (char d : c) {
System.out.print(" " + d + " ");
if (Character.getType(d) == 6) System.out.print(" "); //extra space to make combining diacritics display rightly (NON_SPACING_MARK)
}
System.out.println();
for (char d : c) {
int ic = (int) d;
System.out.printf("%1$#05x ", (int) d);
}
System.out.println();
}
}
下面是输出:
αβγδεζηθικλμνξοπρσςτυφχψω Type the word above: αβγδεζηθικλμνξοπρσςτυφχψω This is what the computer took from the keyboard: αβγδεζηθικλμνξοπÏ�σςτυφχψω Î ± Î ² Î ³ Î ´ Î µ Î ¶ Î · Î ¸ Î ¹ Î º Î » Î ¼ Î ½ Î ¾ Î ¿ Ï € Ï � Ï ƒ Ï ‚ Ï „ Ï … Ï † Ï ‡ Ï ˆ Ï ‰ 0x0ce 0x0b1 0x0ce 0x0b2 0x0ce 0x0b3 0x0ce 0x0b4 0x0ce 0x0b5 0x0ce 0x0b6 0x0ce 0x0b7 0x0ce 0x0b8 0x0ce 0x0b9 0x0ce 0x0ba 0x0ce 0x0bb 0x0ce 0x0bc 0x0ce 0x0bd 0x0ce 0x0be 0x0ce 0x0bf 0x0cf 0x20ac 0x0cf 0xfffd 0x0cf 0x192 0x0cf 0x201a 0x0cf 0x201e 0x0cf 0x2026 0x0cf 0x2020 0x0cf 0x2021 0x0cf 0x2c6 0x0cf 0x2030 This is what it should look like: αβγδεζηθικλμνξοπρσςτυφχψω α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ ς τ υ φ χ ψ ω 0x3b1 0x3b2 0x3b3 0x3b4 0x3b5 0x3b6 0x3b7 0x3b8 0x3b9 0x3ba 0x3bb 0x3bc 0x3bd 0x3be 0x3bf 0x3c0 0x3c1 0x3c3 0x3c2 0x3c4 0x3c5 0x3c6 0x3c7 0x3c8 0x3c9
谁能告诉我如何解决这一问题?
+1用于解释两端的重要性:提供者 - 控制台和读者 - 适用于System.in的编码。 – nhahtdh
感谢您的回复,QM。不幸的是[ISO-8859-1](http://en.wikipedia.org/wiki/ISO/IEC_8859-1)不编码希腊文。所以我需要找到一些方法来弄清楚如何将我的Win7机器设置为UTF8。 (我已经将Eclipse设置为UTF8,因为听起来你需要对你的编辑器做这件事。) – JohnK
我确实发现:[在Windows 7中将UTF8设置为默认字符编码?](http://superuser.com/questions/239810/setting-utf8-as-default-character-encoding-in-windows-7)我已经对Eclipse提出了修改 - 不需要帮助。唉它说,没有办法 设置整个操作系统的编码。 让我回到以前的地方。你可以提供的其他任何指导? – JohnK