0

I know that java 8 uses UTF-8 as standard encoding. When i read string from standard-input(System.in) with Scanner using "UTF-8" then try to print it to console and the string is not in right origin as I show beneath ,but with the initialized string the console show right original string without unexpected result.How can i fix this?Please help me!
I use Scanner not String constructor to encode input.I know how to convert input to UTF-8 but something wrong happened to my result and asked for the reason and solution to solve that problem ,not asking for the way to encode string with UTF-8.

Here is my code:

import java.nio.charset.Charset;
import java.util.Scanner;


public class charset {

    public static void main(String[] args) {
        System.out.println(Charset.defaultCharset());
        Scanner sc =new Scanner(System.in,"UTF-8");
        System.out.println(sc.nextLine());
        System.out.println("á ă ấ");
    }
}

-Here is my output:

run:

UTF-8

á ă ấ

� ? ?

á ă ấ

BUILD SUCCESSFUL (total time: 10 seconds)
  • Possible duplicate of [Encode String to UTF-8](https://stackoverflow.com/questions/5729806/encode-string-to-utf-8) – Naman Sep 12 '17 at 15:18
  • I use Scanner not String constructor to encode input.I know how to convert input to UTF-8 but something wrong happened to my result and asked for the reason and solution to solve that problem ,not asking for the way to encode string with UTF-8. – Mạnh Nguyễn Sep 12 '17 at 16:41

1 Answers1

0

try this one set utf8 eclipse If its not helping try to read from file instead of console to see what happens.