I’ve wanted to make an utf 8 supported chatroom, but it didn’t work only for some specific utf 8 characters, so after spending a week in pure frustration, i narrowed it down to something being wrong with my user input handling, i asked chatgpt too and read countless forums but i couldn’t figure it out.
I’m on Windows, I use vscode, updated version, the terminal there uses utf8 encoding i checked with chcp – returns 65001, same goes for cmd, so i don’t think its a problem with the terminal, i tried brute forcing java System.out to be in utf-8 didn’t fix it (System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8));)
I don’t have a problem when i have preset an utf8 String and print that out
for example:
String random = “háló”;
System.out.println(random);
returns:
háló
I’ve tried Scanner, BufferedReader, InputStreamReader, converting to bytes … im frustrated please help
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, StandardCharsets.UTF_8)
);
System.out.println("Enter some text (UTF-8 characters supported):");
String userInput = reader.readLine();
// Print the user input to verify
System.out.println("You entered: " + userInput);
// Print UTF-8 byte representation for each character
System.out.println("Character: UTF-8 bytes:");
for (int i = 0; i < userInput.length(); i++) {
char c = userInput.charAt(i);
System.out.print(c + ": ");
byte[] bytes = String.valueOf(c).getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
System.out.print((b & 0xFF) + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
returns:
Enter some text (UTF-8 characters supported):
háló
You entered: hl
Character: UTF-8 bytes:
h: 104
: 0
l: 108
: 0
note: im a beginner java dev, coming from python
Ádám is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.