Is there a correct way to write this code?

Asked

Viewed 74 times

2

package linguagem;
import java.util.*;
public class Linguagem {
    public static void main(String[] args) {
        Locale locale = new Locale("PORTUGUESE", "PT");
        System.out.println("Linguagem: " + locale.getDefault());
    }
}

inserir a descrição da imagem aqui

With the locale.getDefault() returning en_US, the use of Locale("PORTUGUESE", "PT"), however, if I just let Locale(), Netbeans Returns Me Error.

And if I leave the string empty: Locale("", ""), It works, but it looks like.

I wanted to know if there is another way to write this code, without having to leave the variable empty.

  • new Locale("portuguese", "pt") nay returns a locale that corresponds to the Portuguese language. The problem is that the constructor does not validate the entry and accepts anything. Correct for Portuguese from Portugal is new Locale("pt", "PT")

1 answer

0


The method getDefault() is static and will always return to JVM default Locale.

I believe what you really want is:

Locale locale = new Locale("portuguese", "pt");
System.out.println("Linguagem: " + locale);

Or, to catch the Locale default:

Locale locale = Locale.getDefault();
System.out.println("Linguagem: " + locale);

Browser other questions tagged

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