[英]How to iterate multiple String input in Java using the Scanner class
我需要從 Java 中的用戶獲取多行輸入。 目前我正在使用 Scanner 類來獲取輸入,但我用來檢查每一行的循環不會中斷。
當前代碼
package getxml;
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
System.out.println("Enter the Codes: ");
Scanner in = new Scanner(System.in);
while(in.hasNextLine()) {
String nextLine = in.nextLine();
System.out.println("nextLine:" + nextLine);
}
in.close();
System.out.println("Scanner closed");
}
}
預期輸入
10229892
10120470
10295277
10229618
10229643
10229699
預期產出
nextLine:10229892
nextLine:10120470
nextLine:10295277
nextLine:10229618
nextLine:10229643
nextLine:10229699
Scanner closed
實際輸出
nextLine:10229892
nextLine:10120470
nextLine:10295277
nextLine:10229618
nextLine:10229643
nextLine:10229699
我試過改變
hasNextLine()
到
hasNext()
並添加一個 If 條件以使用以下邏輯跳出循環:
if (nextLine.equals("")) {
in.close();
System.out.println("bye");
break;
}
我需要將 in.nextLine() 保護到一個變量,否則它將在一個循環中迭代 2 行。
問題是您的代碼不知道用戶是否已停止輸入代碼。
你可以這樣做:
public static void main(String[] args) {
System.out.println("Enter the Codes: ");
Scanner in = new Scanner(System.in);
String nextLine = "";
do{
nextLine = in.nextLine();
System.out.println("nextLine:" + nextLine);
} while(!nextLine.equals("exit"));
in.close();
System.out.println("Scanner closed");
}
你的控制台看起來像:
首先,您必須決定什么將被視為用戶輸入結束,然后根據該特定條件采取行動。 在下面的小例子中,如果用戶什么都不輸入,那么這將被視為用戶輸入結束。
用戶輸入的所有代碼都存儲在一個列表接口對象中。 還應用了規則,而提供的所有代碼必須是數字且長度為八位數字。 String#matches()方法與一個小的正則表達式(regex)一起用於此目的。 此外,不能提供重復的代碼編號,每個代碼編號必須是唯一的。
當用戶完成輸入所需的代碼編號后,這些編號將按升序排列並顯示在控制台窗口中:
System.out.println("Enter the required code numbers (enter nothing when done): ");
Scanner in = new Scanner(System.in);
List<String> codes = new ArrayList<>();
// Outer loop to keep asking for Code Numbers
while (true) {
boolean isValid = false; // Flag to ensure a valid entry
String codeLine = "";
// inner loop to back up valitity before storing supplied code Number.
while (!isValid) {
System.out.print("Code Line: --> ");
codeLine = in.nextLine().trim();
// Break out of this inner loop if nothing is supplied
if (codeLine.isEmpty()) { break; }
// Is the supplied number all digits and are there 8 of them?
if (!codeLine.matches("^\\d{8}$")) {
// No...
System.err.println("Invalid Code Number! Try Again...");
}
// Has the supplied number already been previously stored?
// In other words, is it unique?
else if (codes.contains(codeLine)) {
// Already got it! Not Unique!
System.err.println("Code Number: " + codeLine + " has already been supplied!");
}
// Passed validity! Set isValid to true so to exit this inner loop.
else { isValid = true; }
}
// Break out of the outer loop is nothing was supplied.
if (codeLine.isEmpty()) { break; }
// Validity has been met so Store the the supplied code number.
codes.add(codeLine);
}
in.close(); // Close the input Stream
System.out.println("Scanner closed");
// Sort the Stored Code Numbers in ascending order.
Collections.sort(codes);
// Display the Stored Code Numbers...
System.out.println("Code Numbers Entered:");
System.out.println(codes);
“回車”是輸入結束。 你要做的是模擬或解決這個問題。 如果您的目標只是模擬多行輸入,以下應該有效:
public static void main(String[] args) {
System.out.println("Enter the Codes: ");
Scanner in = new Scanner(System.in);
String allLines = "";
while(in.hasNextLine()) {
String nextLine = in.nextLine().trim();
if (nextLine.isEmpty() || nextLine.equals("\r") || nextLine.equals("\r\n") || nextLine.equals("\n")) {
break;
}
allLines += "\r\n" + nextLine;
//System.out.println("nextLine:" + nextLine);
}
System.out.println("all lines:" + allLines);
in.close();
System.out.println("Scanner closed");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.