如何使用Scanner搜索字符串数组?

时间:2015-04-10 20:07:19

标签: java arrays string

我正在尝试从用户输入中搜索字符串数组,但是我遇到了很多麻烦。运行此程序时,没有任何内容打印到控制台。任何形式的反馈将不胜感激。

 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;
        }
    }

1 个答案:

答案 0 :(得分:0)

好的,然后使用它:

// 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!");