1

Part of my code:

Scanner in = new Scanner(System.in); String s = in.nextLine(); StringBuilder sb = new StringBuilder(s.length()); sb.append(s); 

If add

System.out.println(sb); 

on line 5, it will output � if I enter special character (åäö) at line 4:

String s = in.nextLine(); 

Why is it behaving like this? Should I use something else to get input from the user?

EDIT: It will output "åäö" corretly if I just append "åäö" to a StringBuild and print it with System.out.println(sb); (where sb is the StringBuilder).

EDIT 2: I am using NetBenas IDE 8.0

EDIT 3: The problem does not seem to be the encoding.

Scanner sc = new Scanner(System.in,"utf-8"); 

Still gives the same result.

1
  • 1
    have you looked for encodings? Commented Sep 16, 2014 at 11:19

2 Answers 2

1

What IDE are you using. Try set the console to print UTF-8 chars. If you are using Eclipse go in Run Configurations --> select your Run Configuration --> Under Common tab change the encoding

Sign up to request clarification or add additional context in comments.

13 Comments

It will output "åäö" corretly if I just append "åäö" to a StringBuild and print it with System.out.println(sb);
Using Eclipse:Delete the file. Copy the same file again. Don't open the file. Right Click on the file --> Properties. Resource --> Text file encoding --> check "Other" with UTF-8. Open the file.
I'm using NetBeans IDE 8.0 (Removing this as this was not correct)
btw I have run your code in Eclipse and it works fine. properly printing åäö
I've run you program here is the output of it: åäö å ä ö ÅäÖ
|
0

Strings are always encoded as UTF-16 in java. Set the encoding of your console properly. If you are using some IDE then just google on how to set UTF-8 encoding for console

1 Comment

Looking at build.xml, that seems to be the case already: <?xml version="1.0" encoding="UTF-8"?>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.