2016-10-31 52 views
-1

我有一個定義爲美國的國家/資本(如得克薩斯州/奧斯汀)查找鍵/值,並返回相應的值/關鍵

鍵/值的地圖對象當用戶鍵入一個國家,我需要返回資本,當用戶類型輸入資本我返回狀態

我編碼以下,但它只適用於首都 - >國家比較。

有什麼想法嗎?

public class StatesCapitals extends AppCompatActivity { 

    Map<String, String> capitals = new HashMap<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_states_capitals); 

     String text = "US_states"; //text file in the assets folder 

     byte[] buffer = null; 

     InputStream is; 
     try { 
      is = getAssets().open(text); 
      int size = is.available(); //size of the file in bytes 
      buffer = new byte[size]; //declare the size of the byte array with size of the file 
      is.read(buffer); //read file 
      is.close(); //close file 

     } catch (IOException e) { 
      e.printStackTrace(); 

     } 

     // Store text file data in the string variable 
     String str_data = new String(buffer); 

     //Split using the delimiter ":" to the all elements 
     String[] stateCapsArray = str_data.split(":"); 
     //Iterate over the array 
     for(int i=0;i<stateCapsArray.length-1;i++) { 
      //Skip each other element as we are collecting 2 elements at a time 
      if(i%2 == 0) { 
       String state = stateCapsArray[i]; 
       String capital = stateCapsArray[i+1]; 
       capitals.put(state, capital); 
      } 
     } 

Toast.makeText(getBaseContext(), capitals.toString(), Toast.LENGTH_LONG).show(); 

    } 

    public void doit(View v) 
    { 
     TextView result=(TextView)findViewById(R.id.textViewResult); 
     EditText et=(EditText)findViewById(R.id.editText); 
     String input=et.getText().toString(); 
     Toast.makeText(getBaseContext(), "Input: "+input, Toast.LENGTH_LONG).show(); 

     Iterator entries = capitals.entrySet().iterator(); 
     while (entries.hasNext()) { 
      Map.Entry entry = (Map.Entry) entries.next(); 
      String key = (String)entry.getKey(); 
      String value = (String)entry.getValue(); 
      if (input.equals(key)) 
      {result.setText("The Capital of "+key+" is: "+value);} 
      else if (input.equals(value)) {result.setText("The State of: "+value+" is: "+key);} 
     } 

    } 

} 
+0

數據在文件中看起來像什麼?你能舉一個例子嗎? –

+0

創建兩個地圖,每個方向一個,或谷歌「[Java雙向地圖](https://www.google.com/search?q=Java+Bidirectional+Map)」。 – Andreas

+0

阿拉巴馬州:蒙哥馬利: 阿拉斯加:朱諾: 亞利桑那州鳳凰城: 阿肯色州:小石城: 加利福尼亞州:薩克拉門託國王隊: 科羅拉多州丹佛: 康涅狄格:哈特福德: 特拉華:多佛: 佛羅里達州:塔拉哈西: 佐治亞州亞特蘭大: – christopheUS

回答

0

貌似問題是在循環:

String[] stateCapsArray = str_data.split(":"); 
for(int i=0;i<stateCapsArray.length-1;i++) { 
    //Skip each other element as we are collecting 2 elements at a time 
    if(i%2 == 0) { 
     String state = stateCapsArray[i]; 
     String capital = stateCapsArray[i+1]; 
     capitals.put(state, capital); 
    } 
} 

如果str_datastate1:capital1:state2:capital2:state3:...保持着信息,然後你的循環應該遍歷它爲:

for (int i = 0; i < stateCapsArray.length; i += 2) { 
    String state = stateCapsArray[i]; 
    String capital = stateCapsArray[i+1]; 
    capitals.put(state, capital); 
} 

主要的變化是, i在每次迭代中前進2

相關問題