The HashMap class in Java is represented as a dictionary class, where Key,value one by one corresponds to the principle. Therefore, it is the first tool of dictionary query. (HashMap dictionary class literal meaning can also be seen ~ ~)
Program Ideas:
Before the program starts, you should create a dictionary literal for the storage of the word thesaurus.
Read the text file first, because each behavior is a word and its interpretation, so the method is read by line.
Separate the words and meanings of each line and deposit the two parts into the HashMap object respectively.
Finally enter the query word, find the key in key to get the interpretation.
The code is as follows:
ImportJava.io.*;ImportJava.util.*; Public classHash_map { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); File fi=NewFile ("Dict.txt"); Try{BufferedReader dict=NewBufferedReader (NewInputStreamReader (NewFileInputStream (FI))); String Line_text=""; String Unknow; HashMap Find_word=NewHashMap (); intI=0; while(i<562) {Line_text=Dict.readline (); //System.out.println (line_text);String[] _map=line_text.split (""); //System.out.println (_map[0]+_map[_map.length-1]);Find_word.put (_map[0], _map[_map.length-1]); I+=1; } System.out.println ("Query the first letter of a word!" When the input is Q, launch the program! "); System.out.println ("Enter the word you want to find:"); Unknow=In.next (); while(Unknow.equals ("q") = =false) {System.out.println (Find_word.get (Unknow.tolowercase ())); System.out.println ("Enter the word you want to find:"); Unknow=In.next (); } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("The query is over!" "); Word_up_find (); } Public Static voidWord_up_find () {Scanner in=NewScanner (system.in); intCount=0; System.out.println ("Determines the number of uppercase letters in a string! To enter Q to end "); String Word=In.next (); while(Word.equals ("q") = =false){ Char[] st=Word.tochararray (); for(inti=0;i<st.length;i++){ if(st[i]>= ' A ' &&st[i]<= ' Z ') Count+=1; } System.out.println (count); System.out.println ("Enter the word to be judged:"); Word=In.next (); } System.out.println ("Judgment is over!" "); }}
Run in a DOS window,
Java Dictionary Query example with a as the beginning of a word