2012-11-28 61 views
3

3個字母的國家代碼我得到異常:java.util.MissingResourceException:找不到CS

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<Country> 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<Country> { 
    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(); 
    } 
} 

請幫我解決這個異常。

+0

什麼行實際上是拋出異常。該代碼沒有行號。 – eh9

+0

這是功課嗎?如果是這樣,你的項目是否正確設置了數據文件? – Ivan

回答

5

基本上它的語言環境似乎沒有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

+0

非常感謝... –

相關問題