0

Sorry if this is painfully obvious, but I am new to Java. I was unable to find anything answering this (at least not that I could understand). What I want to accomplish is for, when the user enters the square root symbol (√), the string to contain said symbol. However, when I print it, it is simply a question mark. Here is my code:

Scanner sc = new Scanner(System.in);
String s = sc.next();
System.out.println(s.substring(0,1));

I am guessing that it has something to do with the scanner as when I set it up like this, it works properly.

String s = "√";
System.out.println(s.substring(0,1));

Is there some (preferably simple) way to accomplish this? My goal is not to print the character that they enter, but rather for it to be read in as . In case anyone is curious. I am currently working on a simple calculator with a couple functions (including the square root). Obviously, I could have the user type something like "sqrt" instead, but I would prefer to do it this way.

Thank you in advance.

A Sdi
  • 665
  • 2
  • 10
  • 24
  • Possible duplicate of [Read/write .txt file with special characters](http://stackoverflow.com/questions/4597749/read-write-txt-file-with-special-characters) – VGR Dec 15 '16 at 17:18
  • The problem probably is not your code; it’s that your output is being printed in a console that cannot display that character. – VGR Dec 15 '16 at 17:19
  • @VGR My console displays that character in my second example. – Java Newbie Dec 15 '16 at 17:50
  • Oops, I misread. My mistake. – VGR Dec 15 '16 at 17:51

2 Answers2

0

I don't believe the Scanner class reads UTF-8 encoded text by default, so replace your scanner instantiation with:

Scanner sc = new Scanner(System.in, "utf-8");
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
0

Seems that your input charset is not UTF-8. Try this:

Scanner s = new Scanner(new InputStreamReader(System.in,Charset.defaultCharset()));
marknorkin
  • 3,904
  • 10
  • 46
  • 82
  • Are there any imports I need? Both InputStreamReader and Charset say "cannot find symbol." – Java Newbie Dec 15 '16 at 16:49
  • @JavaNewbie What version of java are you using ? Charset class was added in Java 7: https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html – marknorkin Dec 15 '16 at 16:58
  • I believe NetBeans is using Java 7. Under IDE Log, Java, it says 1.7.0. – Java Newbie Dec 15 '16 at 17:48
  • @JavaNewbie ok, maybe IDE issue. try to use fully qualified class name e.g. java.nio.charset.Charset.defaultCharset() – marknorkin Dec 15 '16 at 17:55
  • That took care of "cannot find symbol" error, but the result is the same as before: a question mark. – Java Newbie Dec 15 '16 at 18:45
  • @JavaNewbie well, I ran your sample and it worked for me, so it's about the encoding. Maybe your IDE uses different encoding. Which one do you use ? – marknorkin Dec 15 '16 at 20:34
  • Also as I'm not an expert in NetBeans, but you may try following answer: http://stackoverflow.com/questions/7219249/netbeans-console-does-not-display-bangla-unicode-characters – marknorkin Dec 15 '16 at 20:34