English is not my first language. Sorry in advance if there's something unclear.
I'm working on a college assignment using Enum and Google Guava Multimap. In my previous assignment(which is almost the same as the one I'm working on), I wrote down a bunch of words. Word for key, and definition for value. I used a scanner to type in the string value and check if there was anything matched in the enum. In the current assignment, I have to add 'part of the speech'. But, I'm not sure what/how to do that.
public class Test1
{
private enum Entry
{
BOOK("Book","noun", "A written work published in printed or electric form."),
BOOK2("Book","verb","To arrange for someone to have a seat on a plane."),
BOOKABLE("Bookable","adjective","Can be ordered in advance."),
BOOKCASE("Bookcase","noun","A piece of furniture with shelves."),
BOOKBINDER("Bol okBinder","noun","A person who fastens the pages of books."),
CSC2201("CSC220","noun","Data Structures."),
CSC2202("CSC220","adjective","Ready to create complex data structures."),
CSC2203("CSC220","verb","To create data structures.");
private String key;
private String partOfSpeech;
private String definition;
private Entry() {}
Entry(String key, String partOfSpeech, String definition)
{
this.key = key;
this.partOfSpeech = partOfSpeech;
this.definition = definition;
}
public String getKey()
{
return this.key.toUpperCase();
}
public String getPartOfSpeech()
{
return this.partOfSpeech;
}
public String getDefinition()
{
return this.definition;
}
public String toString()
{
return this.key+" ["+ this.partOfSpeech+"] : "+this.definition;
}
}
public static void main(String[]args)
{
ListMultimap<String, String> dictionary = ArrayListMultimap.create();
dictionary.put(Entry.BOOK.getKey(),Entry.BOOK.getDefinition());
dictionary.put(Entry.BOOK2.getKey(), Entry.BOOK2.getDefinition());
dictionary.put(Entry.BOOKABLE.getKey(), Entry.BOOKABLE.getDefinition());
dictionary.put(Entry.BOOKCASE.getKey(), Entry.BOOKCASE.getDefinition());
dictionary.put(Entry.BOOKBINDER.getKey(), Entry.BOOKBINDER.getDefinition());
dictionary.put(Entry.CSC2201.getKey(), Entry.CSC2201.getDefinition());
dictionary.put(Entry.CSC2202.getKey(), Entry.CSC2202.getDefinition());
dictionary.put(Entry.CSC2203.getKey(), Entry.CSC2203.getDefinition());
ListMultimap<String,String> partOfSpeechDict = ArrayListMultimap.create();
partOfSpeechDict.put(Entry.BOOK.getKey(),Entry.BOOK.getPartOfSpeech());
partOfSpeechDict.put(Entry.BOOK2.getKey(), Entry.BOOK2.getPartOfSpeech());
partOfSpeechDict.put(Entry.BOOKABLE.getKey(), Entry.BOOKABLE.getPartOfSpeech());
partOfSpeechDict.put(Entry.BOOKCASE.getKey(), Entry.BOOKCASE.getPartOfSpeech());
partOfSpeechDict.put(Entry.BOOKBINDER.getKey(),Entry.BOOKBINDER.getPartOfSpeech());
partOfSpeechDict.put(Entry.CSC2201.getKey(), Entry.CSC2201.getPartOfSpeech());
partOfSpeechDict.put(Entry.CSC2202.getKey(), Entry.CSC2202.getPartOfSpeech());
partOfSpeechDict.put(Entry.CSC2203.getKey(), Entry.CSC2203.getPartOfSpeech());
System.out.println("- DICTIONARY JAVA Standard -----");
System.out.println("----- powered by Google Guava -");
String searchKey = "null";
String endCode = "!q";
do
{
Scanner scan = new Scanner(System.in);
System.out.print("Search: ");
searchKey = scan.nextLine();
searchKey = searchKey.toUpperCase();
if(dictionary.containsKey(searchKey))
{
char[]charArray = searchKey.toCharArray();
for(int i=1;i<charArray.length;i++)
charArray[i]=Character.toLowerCase(charArray[i]);
String str = new String(charArray);
System.out.println(" |");
iterate(dictionary,str);
System.out.println(" |");
}
else if(searchKey.equalsIgnoreCase(endCode))
{
break;
}
else
{
System.out.println(" |");;
System.out.println("\t<Not found>");
System.out.println(" |");
}
}while(!searchKey.equalsIgnoreCase(endCode));
System.out.println("\n-----THANK YOU-----");
}
public static <K,V> void iterate(ListMultimap<String, String> alMultimap,String str)
{
for(Map.Entry<String,String> entry : alMultimap.entries())
{
if(str.equalsIgnoreCase(entry.getKey()))
{
char[]charArray = entry.getKey().toCharArray();
for(int i=1;i<charArray.length;i++)
charArray[i]=Character.toLowerCase(charArray[i]);
String str2 = new String(charArray);
System.out.print("\t"+str2+" ["+getPartOfSpeech(partOfSpeechDict, str2)+"] : "+entry.getValue()+"\n");
//IDE said that error is java : cannot find symbol(partOfSpeechDict). However, I made it.
}
}
}
public static <K,V> String getPartOfSpeech(ListMultimap<String, String> alMultimap,String str)
{
for(Map.Entry<String,String> entry : alMultimap.entries())
{
if(str.equalsIgnoreCase(entry.getKey()))
{
return entry.getValue();
}
}
return null;
}
}
To handle the problem, I made two ListMutimap<String, String> name = ArrayListMultimap.create(). Both of their keys are words, but the first map's value is a definition, and the second map's value is part-of-speech. I'm trying to use the method 'getPartOfSpeech' to get only the value(partOfSpeech) from the second map. Then add that value into the printing line in 'iterate', so it will print out the name, partOfSpeech, and definition. However, IDE said it can't find a symbol: "partOfSpeechDict".
What did I do wrong? Can I not make two maps using Guava Multimap? Please let me know.
Thank you.
I also uploaded about error message, but stackOverflow said there's a related question and decreased my reputation, so I deleted it TT :(
You declare your partOfSpeechDict
in main
method and then call it in iterate
method, which is not able to because of the variable scope inside a method. you can not create a variable in this method and then use it in another method out of nothing like that
If you want to use the partOfSpeechDict
map in your iterate
method, add it to the parameters just like you did it with the dictionary
map
public static void iterate(ListMultimap<String, String> alMultimap,
ListMultimap<String,String> partOfSpeechDict, // include your map here to use it
String str) {
for(Map.Entry<String,String> entry : alMultimap.entries()) {
if(str.equalsIgnoreCase(entry.getKey())) {
char[]charArray = entry.getKey().toCharArray();
for(int i=1;i<charArray.length;i++)
charArray[i]=Character.toLowerCase(charArray[i]);
String str2 = new String(charArray);
System.out.print("\t"+str2+" ["+getPartOfSpeech(partOfSpeechDict, str2)+"] : "+entry.getValue()+"\n");
}
}
}