Allow special characters like Java keyboard input

4

I'm using Scanner to read some keyboard data, but accents and special characters are not identified.

For example: John and Maurice appear with a square in the accented letter, but if I type these words in the output on screen does not have problems. They appear right.

I tried with static Locale PORTUGUESE; but it did not work.

How can I make it work?

Thank you

EDIT

My code is like this:

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
    
asked by anonymous 17.11.2015 / 21:34

2 answers

4

Use the Scanner constructor with encoding overhead as follows: Scanner skener=new Scanner(file,"ISO-8859-1"); if you are reading from a file or Scanner skener=new Scanner(System.in,"ISO-8859-1"); if you are reading from the default input. >

Here's 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());
    }
}
    
17.11.2015 / 21:41
0

I tested it here and it worked (based on the colleague's response):

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 encoding of the Command Prompt.

There is also the option to modify the coding of the command prompt with the chcp 1252 command (for Windows-1252 encoding) after it has been opened or call it already with this encoding with the command cmd.exe 1252 .

Source and more information: Spice characters Command Prompt

    
19.11.2015 / 12:47