java request 国家代码_java.util.MissingResourceException:找不到CS的3个字母的国家/地区代码...

我得到了例外:

Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Couldn't find 3-letter country code for CS

at java.util.Locale.getISO3Country(Locale.java:1521)

at Business.CountryList.CountryList(CountryList.java:29)

我把这个函数称为:

countryJComboBox.removeAllItems();

countryJComboBox.addItem(CountryList.CountryList(new String[0]));

CountryList类如下:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package Business;

import java.text.Collator;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Locale;

/**

*

* @author Vivek

*/

public class CountryList {

public static Country CountryList(String[] args) {

List countries = new ArrayList<>();

Locale[] locales = Locale.getAvailableLocales();

for (Locale locale : locales) {

String iso = locale.getISO3Country();

String code = locale.getCountry();

String name = locale.getDisplayCountry();

if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {

countries.add(new Country(iso, code, name));

}

}

Collections.sort(countries, new CountryComparator());

Country returnCountry = null;

for (Country country : countries) {

returnCountry = country;

}

return returnCountry;

}

}

class CountryComparator implements Comparator {

private Comparator comparator;

CountryComparator() {

comparator = Collator.getInstance();

}

@Override

public int compare(Country o1, Country o2) {

return comparator.compare(o1.name, o2.name);

}

}

class Country {

private String iso;

private String code;

public String name;

Country(String iso, String code, String name) {

this.iso = iso;

this.code = code;

this.name = name;

}

@Override

public String toString() {

return iso + " - " + code + " - " + name.toUpperCase();

}

}

请帮我修复异常.

解决方法:

基本上它所在的语言环境似乎没有3个字母的国家代码.因为你试图忽略那些没有3字母iso代码的国家:

for (Locale locale : locales) {

try {

String iso = locale.getISO3Country();

String code = locale.getCountry();

String name = locale.getDisplayCountry();

if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {

countries.add(new Country(iso, code, name));

}

catch (MissingResourceException e){

//do nothing

}

}

有关更多信息,请参见Java Docs

标签:jcombobox,java,swing,country-codes

来源: https://codeday.me/bug/20190901/1783828.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值