how can I make Locale output that the language of the passport is Russian, and for each element in the array list, to call the method isRussian();
public class Passport implements Comparable<Passport>{
private String firstName;
private String lastName;
private Locale passportIssue;
// c-tor
public Passport(String firstName, String lastName, Locale passportIssue){
this.firstName = firstName;
this.lastName = lastName;
this.passportIssue = passportIssue;
}
//method
public boolean isRussian() {
If(passportIssue== "ru"){
return true;
}
return false;
}
}
This is what i have try
List<Passport> pass= new ArrayList<>();Locale a= new Locale("ru");
Passport p = new Passport("Erika",Ivanovich",new Locale("ru"));
Passport p1 = new Passport ("Kemal", "Remzi", Locale.ENGLISH)
Passport p2 = new Passport ("Ivana",Bogdan",new Locale("ru"));
pass.add(p);
pass.add(p1);
pass.add(p2);
System.out.println("Is the passport " p.getPassIssue() " russian? "
CodePudding user response:
Locale :: getLanguage
A Locale
is composed of a culture and a language.
Apparently you want to compare just the language portion. So get the language code by calling Locale :: getLanguage
.
The Javadoc warns that these two-letter codes have been changing. So compare by constructing a new Locale
object using your expected code, and then extract the possibly transformed code.
if ( locale.getLanguage().equals( new Locale( "ru" ).getLanguage() ) ) { … }
Or if your intent was the culture portion, access the culture portion using Locale :: getCountry
. But keep in mind that Locale
is meant for localization usage, not legal usage. The “country” in a Locale
is a way of representing culture, not a government or nation-state.
Do not compare strings using ==
, which tests the objects’ reference, not the contained text.
CodePudding user response:
You are comparing a string with a Locale in the following line (using the "==" operator, which checks if they are the same instance, and that is never the case)
public boolean isRussian() {
return passportIssue.getCountry().equals("RU");
}
Please note, getCountry()
returns the country code in upper case (see the documentation here.