简体   繁体   English

如何使用扫描仪搜索字符串数组?

[英]How do I use Scanner to search an array of Strings?

I am trying to search an array of strings from a user input but I am getting quite a bit of trouble. 我正在尝试从用户输入中搜索字符串数组,但遇到了很多麻烦。 Nothing is printed to console when i run this program. 当我运行此程序时,没有任何内容打印到控制台。 Any sort of feedback would be appreciated. 任何形式的反馈将不胜感激。

 public static void main(String args[]) throws ParseException,
        FileNotFoundException {
    /* Initialization */

    String[] keywords = { " day", "What book", "professor name", " office",
            "hour", "e-mail", "name", "major", "student e-mail",
            "group id", "group name", "lecture", "lecture room",
            "lecture time", "number of lectures", "current lecture",
            "topics of current lecture", "number of test",
            "date of a test", "number of assignments", "sure",
            "current assignment", "due day" };

    String currentkeyword = "";
    boolean endloop = false;
    Scanner darkly = new Scanner(System.in);
    Scanner currentline;
    boolean found = false;
    String response = "";
    String[] response_array = { "" };

    /* -end init- */

    /* Print welcome message */
    System.out.println("Welcome ");
    System.out.println("What's on your mind?");
    /* -end printing- */

    /* Run a loop for I/O */
    while (!endloop) {
        System.out.print(" - ");
        currentline = new Scanner(darkly.nextLine().toLowerCase());
        if (currentline.findInLine("bye") == null) {

            for (int i = 0; i < keywords.length; i++) {
                if ((currentline
                        .findInLine(currentkeyword = (String) keywords[i]) != null)) {

                    found = true;
                    Sytem.out.Println(currentkeyword);
                }
    }
  else {// Exit program if 'bye' was typed
            System.out.println("Alright then, goodbye!");

            // endloop = true;
        }
    }

OK, use this then: 好的,然后使用它:

// here your array of terms
Scanner scanner = new Scanner(System.in);
String input = null;

System.out.println("Welcome ");
System.out.println("What's on your mind?");
do {
  System.out.print("> ");
  input = scanner.next();
  for (int i = 0; i < keywords.length; i++) {
    if (keywords[i].equalsIgnoreCase(input)) {
      System.out.println("Found keyword!");
      // TODO: You can optimize this
    }
  }
} while (!input.equalsIgnoreCase("bye"));
System.out.println("Alright then, goodbye!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM