首页 > 解决方案 > Java 扫描器第二个输入以缩小搜索范围

问题描述

我正在创建一个交互式字典,它将从枚举类中获取外部数据源。我能够使用 HashMap 和 pair 操作成功地创建字典,以将语音类型与其定义结合起来。但是,如果输入了第二个输入,程序应该缩小搜索范围。例如,“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);
}

}

标签: javajava.util.scanner

解决方案


推荐阅读