Allow special characters as Java keyboard input

Asked

Viewed 1,471 times

4

I am using Scanner to read some keyboard data, however, accents and special characters are not identified.

For example: João and Maurício appear with a small square in the accented letter, however, if I type these words in the screen output is no problem. Show up straight.

I tried with static Locale PORTUGUESE; but it didn’t work out.

How can I make it work?

Thank you

EDIT


My code is like this at the moment:

package Trabalho_final;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Funcionario 
{
    private String nome, email, telefone;
    private float salario;

    Scanner ler = new Scanner(System.in,"UTF-8");

          //............ restante do código
  • Put the code you already have so we can help. Another question, is running your program on cmd or in the console da IDE ?

2 answers

4


Use the constructor of Scanner with, overload of encoding, as follows: Scanner skener=new Scanner(file,"ISO-8859-1"); if reading from a file or Scanner skener=new Scanner(System.in,"ISO-8859-1"); if reading from the standard input.

Below is a complete example:

import java.util.Scanner;

public class ScannerCharactersWithAccent {

    public static void main(String[] args) {

        // read line
        System.out.println("Por favor, entre com os caracteres a serem lidos na próxima linha:");
        Scanner scanner = new Scanner(System.in,"ISO-8859-1");
        System.out.println(scanner.nextLine());

        // print line
        System.out.println("Caracteres lidos na linha anterior: " + scanner.nextLine());
    }
}
  • 1

    what would be the file?

  • corrected issue :)

  • If the solution has answered your question, please click the green arrow on top of the answer. Thank you :)

  • Oops, Eduardo. Sorry for the delay, I hadn’t had time to check here yet. So you haven’t solved =/ nor with the encoding overload. Do you have any more suggestions? I’ll put the code up there

  • 2

    Dear @Maximilianomeyer, I modified the answer. I believe it now works. Please test and let me know the result.

0

I tested it here and it worked like this (based on the answer from the colleague):

import java.util.Scanner;

public class Teste {

    public static void main(String[] args) {

        // read line
        System.out.println("Por favor, entre com os caracteres a serem lidos na próxima linha:");
        Scanner scanner = new Scanner(System.in,"CP850");

        // print line
        System.out.println("Caracteres lidos na linha anterior: " + scanner.nextLine());
    }
}

CP850 is the default command prompt encoding.


There is also the option to modify the command prompt encoding via the command chcp 1252 (for Windows-1252 encoding) after the same open or call it already with this encoding with the command cmd.exe 1252.

Source and more information: Special characters Command Prompt

  • Thank you for your contribution, Shura, but I tested it here and it distorted the characters. "til", for example, "ã" became "Ò".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.