public static void main(String[] args) throws IOException {
String str1 = "ΔΞ123456";
System.out.println(str1+"-"+str1.matches("^\\p{InGreek}{2}\\d{6}")); //ΔΞ123456-true
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str2 = br.readLine(); //ΔΞ123456 same as str1.
System.out.println(str2+"-"+str2.matches("^\\p{InGreek}{2}\\d{6}")); //Δ�123456-false
System.out.println(str1.equals(str2)); //false
}
The same String doesn’t match regex when read from keyboard.
What causes this problem, and how can we solve this?
Thanks in advance.
EDIT: I used System.console() for input and output.
public static void main(String[] args) throws IOException {
PrintWriter pr = System.console().writer();
String str1 = "ΔΞ123456";
pr.println(str1+"-"+str1.matches("^\\p{InGreek}{2}\\d{6}")+"-"+str1.length());
String str2 = System.console().readLine();
pr.println(str2+"-"+str2.matches("^\\p{InGreek}{2}\\d{6}")+"-"+str2.length());
pr.println("str1.equals(str2)="+str1.equals(str2));
}
Output:
ΔΞ123456-true-8
ΔΞ123456
ΔΞ123456-true-8
str1.equals(str2)=true
If you use Windows, it may be caused by the fact that console character encoding (“OEM code page”) is not the same as a system encoding (“ANSI code page”).
InputStreamReaderwithout explicit encoding parameter assumes input data to be in the system default encoding, therefore characters read from the console are decoded incorrectly.In order to correctly read non-us-ascii characters in Windows console you need to specify console encoding explicitly when constructing
InputStreamReader(required codepage number can be found by executingmode con cpin the command line):The same problem applies to the output, you need to construct
PrintWriterwith proper encoding:Note that since Java 1.6 you can avoid these workarounds by using
Consoleobject obtained fromSystem.console(). It providesReaderandWriterwith correctly configured encoding as well as some utility methods.However,
System.console()returnsnullwhen streams are redirected (for example, when running from IDE). A workaround for this problem can be found in McDowell’s answer.See also: