Java扫描器的第二个输入可缩小搜索范围

时间:2019-02-25 05:02:39

标签: java java.util.scanner

我正在创建一个交互式字典,它将从枚举类中获取外部数据源。我能够使用HashMap和pairs操作成功创建字典,以将语音类型与其定义结合起来。但是,如果输入了第二个输入,则程序应缩小搜索范围。例如,“ Distinct Noun”应仅输出distinct的名词。但是,这仅输出Distinct和Noun的值。我尝试为第二个输入创建第二个扫描仪,但是当我这样做时,第一个输入没有响应。 有人知道为什么会这样吗?有人可以指出我正确的方向吗? 我已经尝试过scan.hasNextLine()和scan.next(),但是我不确定是否正确实现了它们以缩小搜索范围。

我提供了我所获得的输出以及所需输出的示例输出。如果需要,我可以提供我的代码,但是如果你们能指出我正确的方向,我将不胜感激,因为我想自己弄清楚这一点。 任何信息都会有所帮助。

如果我不正确地问了这个问题,请告诉我。这是我的第一篇文章,我听说过关于张贴错误问题或要求家庭作业帮助的故事。但是我认为寻求指导是可以的,对吧?

感谢您的时间,

我的输出:

Search: distinct noun
 |
   Distinct [adjective] : Familiar. Worked in Java.

   Distinct [adjective] : Unique, No duplicates.

   Distinct [adverb] : Unique. Written "distinctly".

   Distinct [noun] : A keyword in this assignment.

   Distinct [noun] : A keyword in this assignment.

   Distinct [noun] : A keyword in this assignment.

   Distinct [noun] : An advanced search option.

   Distinct [noun] : Distinct is a parameter in this assignment.
 |
 Search:  
 |
   Noun [noun] : Noun is a word that refers to a person, place, or thing.
 |

所需的输出:

Search: distinct noun

 |

   Distinct [noun] : A keyword in this assignment.

   Distinct [noun] : A keyword in this assignment.

   Distinct [noun] : A keyword in this assignment.

   Distinct [noun] : An advanced search option.

   Distinct [noun] : Distinct is a parameter in this assignment.
 |

这是我的代码:

public class DictionaryCSC340{

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    ArrayList keyWords = new ArrayList();
    Map<String, List<dictionaryDataBase>> hmap = new HashMap<>();


    //Begin loading data
    System.out.println("! Loading data...");
    //Loop through data in dictionary
    for (dictionaryDataBase entry : dictionaryDataBase.values()) {      // Enhanced for-loop to get values from enum.

        ArrayList pairs = new ArrayList(); //ArrayList to store data speech and definition.

        String[] speech, definition;                 // Strings to store type and desc from enum
        keyWords.add(entry.name());          // Add all "Key names" to ArrayList
        speech = entry.getSpeech();            //string of speeches
        definition = entry.getDefinition();        //string of definitions


        //loop through speeches and pair it with its associated definitions.
        for (int i = 0; i < speech.length; i++) { 
            Pair<String, String> pair = new Pair<>(speech[i], definition[i]);   // Pair speech and definition   
            pairs.add(pair);    // Add pair with speech and definition to Arraylist "Pairs"
        }

        hmap.put(entry.name().toLowerCase(), pairs);       //Store entry names as the key value and speech and definitions as values. 

    }

    System.out.println("! Loading completed...");
    System.out.println("-----DICTIONARY 340 JAVA-----");


    //Interface properties
    ArrayList dataKey;        
    Pair inputPair;
    String input;


    //program begins search.
    do{

        System.out.print("Search: ");
        input = scan.next().toLowerCase();

        String entryName = new String();
        //Loop through keywords to find a match with the input (ignore cases)
        for(int i = 0; i < keyWords.size(); i++){
            if(((String) keyWords.get(i)).equalsIgnoreCase(input)){
                entryName = (String) keyWords.get(i);   
            }     
        }

        dataKey = (ArrayList) hmap.get(input);
        //if input matches with a value within HashMap, print its value.
        if(dataKey != null){
          System.out.println(" |");

          for(int i = 0; i < dataKey.size(); i++){
              inputPair =(Pair) dataKey.get(i);

              System.out.print("   " + entryName + " [" + inputPair.getKey() + "] : " );

              System.out.println(inputPair.getValue());
            }
          System.out.println(" |");

        }
        else if(input.equalsIgnoreCase("!Q")){  //exit operation when !Q is entered.
            System.out.println("-----THANK YOU-----");
            System.exit(0); 
        }
           //for words that are not within the database.
          else{
            System.out.println(" |");
            System.out.println("   <Not Found>");
            System.out.println(" |");
        }
    }
    while(true);
}

}

0 个答案:

没有答案