0

I need to create a code in Java which takes user input (a word) and search through first Array of String, if found the word in that array, print out the meaning of that word from second array.

for example:

First String Array: {"Computer", "Java", "Stack Overflow"} Second String Array: {"Machine","Programming","Amazing people"}

So, if user input is: Computer The print out should be Machine

if user input is: Java The print out should be Programming.

See my code below. No matter what user type, it just give first index from second Array.

I will really appreciate your help.

Thank you in Advance!

Regards,

Vikram

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]); 
    }
}
1
  • Do a system.out.println in the seccond while loop, and print j. I think the program never reaches that. Commented Nov 14, 2014 at 13:41

5 Answers 5

1

This code works for me

    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]);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use a map for key-value pairs:

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) + "'.");

Comments

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]);
        }
    }

Comments

0

Try This is a Simple :

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

Comments

0

For an exact match, just find the index of the search term and pull that out of the definitions by that value. Simplest example is the following code. I didn't use anything other than arrays as that was your requirement. You could also do case insensitive searches by lowercasing (or uppercasing) the search term and array of terms as you search, but that is up to you.

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]);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.