簡體   English   中英

如何使用掃描儀搜索字符串數組?

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

我正在嘗試從用戶輸入中搜索字符串數組,但遇到了很多麻煩。 當我運行此程序時,沒有任何內容打印到控制台。 任何形式的反饋將不勝感激。

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

好的,然后使用它:

// 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