String doesn't match regex when read from keyboard.

Posted by athspk on Stack Overflow See other posts from Stack Overflow or by athspk
Published on 2011-01-02T15:25:39Z Indexed on 2011/01/02 19:53 UTC
Read the original article Hit count: 204

Filed under:
|
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

© Stack Overflow or respective owner

Related posts about java

Related posts about regex