Printstream does not record accents

Asked

Viewed 133 times

2

Could you help me with a question? My code is not accentuating the text file, even passing the encoding. (I am reading it in notepad)

Scanner in = new Scanner(System.in);
private String texto;
public void escrever(){


        try{
            PrintStream ps = new PrintStream("saida.txt","UTF-8");
            for(;;){
            System.out.print(">> ");
            this.setTexto(in.nextLine());

            if(texto.equals("::exit")){
                System.out.print("Saindo...");
                break;
            }else{
                ps.println(this.getTexto());
                }
        }

        }catch(FileNotFoundException | UnsupportedEncodingException e){
            System.out.println(e);
        }
    }
public String getTexto() {
    return texto;
}

public void setTexto(String texto) {
    this.texto = texto;
}

I’m sorry for any failure here. I’m still learning how to use the site. Thanks in advance!

  • Are you entering this information from the operating system console? Which operating system are you using?

  • I am entering through the netbeans terminal. And using windows 10.

  • What is the text that should be printed and how is it being printed? Are you opening it from the regular notepad? Already checked the encoding of the text editor you are using?

1 answer

1


This problem is not caused by Character encoding of PrintScream, but by using different Character encodings between the Scanner and the console/terminal you are using.

To solve this problem you can change the encoding used by Netbeans. I don’t have Netbeans installed here, so I can’t test but, according to that answer in Soen, this is done this way (Note: this will not only change the encoding console, but also files):

Go to the Netbeans folder, open the folder etc, open the file netbeans.conf and add the following line to the variable netbeans_default_options (the values of this variable are marked by "", then check that the code below has been placed between the quotation marks):

-J-Dfile.encoding=UTF-8

Restart the Netbeans IDE.

This will solve the problem when you use the Netbeans terminal, but will not solve it if you use some other terminal, such as Windows, for example. In this case, you should try one of the following two solutions:

Inform the Character enconding Windows console in your constructor Scanner. Example:

Scanner in = new Scanner(System.in, "Cp850");

A disadvantage of the above solution is that it is not portable.

Another solution is to use the class Console to read data entry. Example:

Console console = System.console();
String string = console.readLine();

The disadvantage of this method is that it cannot be used in Ides.

Another solution specific to Netbeans is to change the encoding used by Scanner for Cp1252. So change your statement Scanner for:

Scanner in = new Scanner(System.in, "Cp1252");
  • There must be some problem on my computer, because none of the solutions worked. With python 3 I didn’t have this problem and.e

  • @Wallacerocha forgot to mention in my reply which line -J-Dfile.encoding=UTF-8 should be added in a specific variable, not in any file location. Reread my answer and try again, I tested it in Netbeans and it worked! I also added encoding used by Netbeans.

  • Before I had not understood, but now I did it the right way and it worked. I also tried to use the eclipse, already in this, the problem did not occur. I was just trying to write a "Hello!" hahahaha Thank you!!!

Browser other questions tagged

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