2014-11-14 53 views
0

我需要在Java中創建一個代碼,它接受用戶輸入(一個單詞)並搜索第一個字符串數組,如果在該數組中找到該單詞,則從第二個數組中打印該單詞的含義。Java String Arrays

例如:

第一個字符串數組:{ 「電腦」, 「Java」 的, 「堆棧溢出」} 第二個字符串數組:{ 「機」, 「編程」, 「神奇人」}

因此,如果用戶輸入是:計算機 打印出來應該是機

如果用戶輸入的是:Java的 打印出來應該編程。

看到我的代碼如下。不管用戶類型如何,它只是給第二個數組的第一個索引。

我真的很感謝你的幫助。

先謝謝您!

問候,

維克拉姆

import java.util.Arrays; 

import javax.swing.JOptionPane; 

public class Test { 


    public static void main(String[] args) { 

     String [] MyString = {"Computer","Science","Java", "Coding"}; 
     String [] Meaning = {"Machine","Life","Programming","Programer"}; 
     String MySearch; 
     int i =0; int j =0; 


     MySearch = JOptionPane.showInputDialog(null, "Enter Your Word to Search"); 
     while (i < MyString.length){ 
      if(String.valueOf(MySearch).equals (Arrays.asList(MyString[i]))){ 
       while(j < Meaning.length){ 
        j++; 
       } 
      } 
      i++;   
     } 
     System.out.println(Meaning[j]); 
    } 
} 
+0

在seccond while循環中執行system.out.println,並打印j。我認爲該計劃從未達到過。 – 2014-11-14 13:41:33

回答

1

此代碼對我的作品

import java.util.Arrays; 
    import java.util.List; 
    import javax.swing.JOptionPane; 

    public class Test { 

    public static void main(String[] args) { 

    String[] MyString = { "Computer", "Science", "Java", "Coding" }; 
    String[] Meaning = { "Machine", "Life", "Programming", "Programer" }; 
    String MySearch; 

    MySearch = JOptionPane.showInputDialog(null, "Enter Your Word to Search"); 
    List<String> myStringList = Arrays.asList(MyString); 
    int index = myStringList.indexOf(MySearch); 
    System.out.println(Meaning[index]); 
    } 
} 
0

使用鍵值對的映射:

Map<String, String> map = HashMap<String, String>(); 
map.put("Computer", "Machine"); 
map.put("Science", "Life"); 
map.put("Java", "Programming"); 
map.put("Coding", "Programmer"); 

String userInput = "Java"; 

System.out.println("Meaning of '" + userInput + "' is '" + map.get(userInput) + "'."); 
0
String [] MyString = {"Computer","Science","Java", "Coding"}; 
String [] Meaning = {"Machine","Life","Programming","Programer"}; 
String MySearch; 

MySearch = JOptionPane.showInputDialog(null, "Enter Your Word to Search"); 

for (int i = 0; i < MyString.length; i++) { 
     if (MySearch != null && MySearch.equalsIgnoreCase(MyString[i]) && i < Meaning.length) { 
      System.out.println(Meaning[i]); 
     } 
    } 
0

嘗試這是一個簡單的:

for(i=0;i<MyString.length;i++) 
{ 
    if(MySearch.equalsIgnoreCase(MyString[i])) 
    { 
     System.out.print(Meaning[i]); 
    } 
} 
0

精確匹配,只要找到搜索詞的索引和拉說出來的定義由價值。最簡單的例子是以下代碼。我沒有使用除數組之外的其他任何東西,因爲這是您的要求。您還可以在搜索時將搜索項和術語數組縮小(或大寫)以進行不區分大小寫的搜索,但這取決於您。

public static void main(String[] args) { 

    String [] terms = {"Computer","Science","Java", "Coding"}; 
    String [] definitions = {"Machine","Life","Programming","Programmer"}; 
    String term = JOptionPane.showInputDialog(null, "Enter Your Word to Search"); 

    for (int i = 0; i < terms.length; i++) { 
     if (terms[i].equals(term)) { 
      System.out.println(definitions[i]); 
     } 
    } 
}