0

I'm trying to get the user name as an input, but whenever I run the code, it doesn't show properly. I tried adding Charset.forName("UTF-8"); but it did not solve the problem.

It shows the same problem in Git Bash and Powershell.

import java.util.Scanner;

public class UserInput {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in, Charset.forName("UTF-8"));

        System.out.print("What is your name? ");
        String name = scanner.nextLine();

        System.out.println("Hello " + name);

    }
}

Console:

What is your name? João
Hello Jo?o

I also tried running "chcp 65001" before running the code and it returns:

What is your name? João
Hello Joo

And then I added this code to try to fix it and I got a different error

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class input {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        try {
            System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));
            System.out.print("What is your name? ");
            String name = scanner.nextLine();

            System.out.println("Hello " + name);
        } catch (UnsupportedEncodingException e) {
            throw new InternalError("VM does not support mandatory encoding UTF-8");
        }

    }
}

Console:

What is your name? João 
Hello Jo´┐¢o
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
losdekai
  • 19
  • 4
  • **Comments have been [moved to chat](https://chat.stackoverflow.com/rooms/253205/discussion-on-question-by-losdekai-character-not-showing-properly-in-java); please do not continue the discussion here.** Before posting a comment below this one, please review the [purposes of comments](/help/privileges/comment). Comments that do not request clarification or suggest improvements usually belong as an [answer](/help/how-to-answer), on [meta], or in [chat]. Comments continuing discussion may be removed. – Samuel Liew Apr 18 '23 at 01:31
  • 2
    Does this answer your question? [How can I change the Standard Out to "UTF-8" in Java](https://stackoverflow.com/questions/28567208/how-can-i-change-the-standard-out-to-utf-8-in-java) – JustBeingHelpful Apr 18 '23 at 01:34

1 Answers1

0

Vscode uses the computer's built-in terminal as an integrated terminal, so this has something to do with your machine language, script encoding, terminal code and many other things. You can try to modify the machine language and other operations to get the correct output, but it will be a little troublesome.

Here's another option to try:

  1. Configure launch,json as follows

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "java",
                "name": "Current File",
                "request": "launch",
                "mainClass": "${file}",
                "console": "internalConsole"
            }
        ]
    }
    
  2. Then execute the script with Run Without Debugging (Ctrl+F5)

    enter image description here

  3. Input in the DEBUG CONSOLE panel

    enter image description here

  4. Enter to get the output

    enter image description here

JialeDu
  • 6,021
  • 2
  • 5
  • 24