0

I wrote this code. I am new to Java and willing to develop my skills,so I wrote this code,in order to learn Arrays,one developer suggested HashSet,I am looking forward for new suggestions.

import java.io.*; public class dictionary { public static void main(String args[]) { String[] MyArrayE=new String[5]; String[] MyArrayS=new String[5]; MyArrayE[0]="Language"; MyArrayE[1]="Computer"; MyArrayE[2]="Engineer"; MyArrayE[3]="Home"; MyArrayE[4]="Table"; MyArrayS[0]="Lingua"; MyArrayS[1]="Computador"; MyArrayS[2]="Ing."; MyArrayS[3]="Casa"; MyArrayS[4]="Mesa"; System.out.println("Please enter a word"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String word= null; try { word= br.readLine(); } catch (IOException e) { System.out.println("Error!"); System.exit(1); } System.out.println("Your word is " + word); for(int i=0; i<MyArrayE.length; i++) { if(word.equals(MyArrayS[i])) { System.out.println(MyArrayE[i]); } } } } 

My Question: What about if the user inputs a word not in MyArrayS, I want to check that and print a statement like "Word does not exist".

I think that it might look like:

if(word!=MyArrayS) { System.out.println("Word does not exist"); } 

Thanks

4
  • I get that you're practicing arrays -- that's good. This particular use case is actually well-suited to using a Map (of which HashMap is one particular choice). Set/HashSet doesn't quite work here. Commented Aug 31, 2012 at 21:34
  • @JimN I believe he does not need the indices of the words. So, IMHO, HashSet is best suited here for a dictionary implementation. Basically, he has just a single data type to store (i.e. words). Unless, he wants to store the word meanings or the indices are important, HashMap does not make sense. Commented Aug 31, 2012 at 23:00
  • 2
    @Ankit he has two parallel arrays, one for the Spanish word and the other for the corresponding English word. He is using the index to cross-reference them. So this is a mapping. Commented Sep 1, 2012 at 0:10
  • If you are OK with using libraries apart from the ones in the default JDK, google guava provides a BiMap implementation with which you can access the English word from Spanish word and vice-versa. Commented Sep 1, 2012 at 5:48

2 Answers 2

1

You can simply use the .Contains(String) method to determine whether the word is contained within the array.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to check every element of the array to see that it's not there. So your code would actually look like:

int c; boolean found = false; for(c = 0;c < MyArrayS.length;c++) { if(MyArrayS.compareTo(word) == 0) { found = true; } } if(!found) { System.out.println("Word does not exist"); } 

5 Comments

Also,I found Jexcel API,I learned how to creat an excel sheet,will do this "dictionary" using two columns of excel.
When I enter a word like "Mesa",when there is a space(I hit the space bar) before or/and after the word,it does not recognize the word and it gives :"word does not exist"!! Any help?
When using this approach, you should typically call "break;" after finding the word, to terminate the loop early, as a performance optimization.
In response to your last question (regarding spaces), you may want to normalize the words before you add them to your list, and also normalize the word being queried before you do your search. Normalization may include removing leading/trailing spaces (see String.trim()), converting words to all-lower-case (see String.toLowerCase()), converting nouns to singular form, converting verbs to root form, etc. as your use case warrants.
I used word.trim(),and it removed the spaces before and after the word that I entered,your answer is good.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.