Jave code replaces accented letters by interrogation points

60 Views Asked by At

A very basic problem that I've encountered in a project and it reoccured in this basic code:

import java.util.Scanner;

public class Special {
    public static void main(String[] args) {
        System.out.println("Donner le mot");
        Scanner sc = new Scanner(System.in, "UTF-8");
        String w = sc.next();
        System.out.println(w);
    }
}

So I insert a string containing normal alphabetic letters and accented letters that are used in French (é,ç,ù...), the problem I faced is in the printed output which displays the normal letters as they are and prints an interrogation point "?" at the place of each accented letter. The compilation lines are those as usual:

javac Special.java
java Special

it's noteowrthy that I made sure to save the file in UTF-8 encoding format.

1

There are 1 best solutions below

0
SerhiiH On

Your program is written correctly!

Try checking what your environment settings are. Here is example how you can do it:

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

public class Main {
    public static void main(String[] args) {
        // Get the default charset for the terminal
        Charset terminalCharset = Charset.defaultCharset();
        System.out.println("Terminal Charset: " + terminalCharset.displayName());

        // Get all available charsets for the current system
        Map<String, Charset> availableCharsets = Charset.availableCharsets();
        System.out.println("Available Charsets:");
        for (Map.Entry<String, Charset> entry : availableCharsets.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue().displayName());
        }
    }
}