How to read propositional logic symbols as input using Java Scanner?

372 Views Asked by At
 Scanner in = new Scanner(System.in,"UTF-8");
 System.out.println(in.next());

If I paste , I receive ? as output to the console. Can someone explain what I can do to properly read logic symbols like this? I'm using NetBeans 8.0.1.

Thanks.

2

There are 2 best solutions below

0
Sergey Kalinichenko On BEST ANSWER

The problem is not with entering the character, but rather with printing it to console. Your console does not appear to support the code point \u2227 of , so it prints a question mark instead.

You should test if console lets you input correctly by printing the numeric representation of the character that you read, like this:

Scanner in = new Scanner(System.in,"UTF-8");
String s = in.next();
if (s.length() != 0) {
    System.out.println((int)s.charAt(0));
}

If 8743 gets printed, you could process the character internally: comparisons like this

if (s.equals("∧")) {
    ...
}

will work correctly.

Otherwise, you should switch to using characters from the first code page, i.e. ^ instead of

0
ingrid.e On

I think your terminal encoding ( IDE or command line ) might not be UTF-8 so when it converts you see the difference.

I just tried your code and got ∧ back.

Do an echo $LC_CTYPE or echo $LANG to check it.

Read this How to read a text file with mixed encodings in Scala or Java? and http://docs.oracle.com/javase/7/docs/api/java/nio/charset/CharsetDecoder.html
for working with different encodings.