如何使用扫描仪 class 在 Java 中迭代多个字符串输入
How to iterate multiple String input in Java using the Scanner class
我需要从 Java 中的用户获取多行输入。
目前我正在使用 Scanner class 来获取输入,但是我正在使用的循环
检查每一行没有中断。
当前代码
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");
}
您的控制台将如下所示:
首先,您必须决定要考虑什么用户输入结束,然后根据该特定条件采取行动。在下面的小例子中,如果用户输入 nothing 那么这将被视为 用户输入结束.
用户输入的所有代码都存储在列表界面对象中。还应用了规则,其中提供的所有代码编号 必须 是 数字 并且 长度为八位数字 。 String#matches() method is used for this along with a small Regular Expression(正则表达式)。此外,不能提供重复的代码编号,每个代码编号必须 唯一。
当用户完成输入所需的代码后,这些数字将按升序排序并显示在控制台中 window:
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);
“Enter”是输入结束。您正在尝试做的是 simulate 或解决该问题。如果您的 objective 是 只是模拟 多行输入,则以下应该有效:
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");
}
我需要从 Java 中的用户获取多行输入。 目前我正在使用 Scanner class 来获取输入,但是我正在使用的循环 检查每一行没有中断。
当前代码
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");
}
您的控制台将如下所示:
首先,您必须决定要考虑什么用户输入结束,然后根据该特定条件采取行动。在下面的小例子中,如果用户输入 nothing 那么这将被视为 用户输入结束.
用户输入的所有代码都存储在列表界面对象中。还应用了规则,其中提供的所有代码编号 必须 是 数字 并且 长度为八位数字 。 String#matches() method is used for this along with a small Regular Expression(正则表达式)。此外,不能提供重复的代码编号,每个代码编号必须 唯一。
当用户完成输入所需的代码后,这些数字将按升序排序并显示在控制台中 window:
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);
“Enter”是输入结束。您正在尝试做的是 simulate 或解决该问题。如果您的 objective 是 只是模拟 多行输入,则以下应该有效:
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");
}