英文:
how to stop while loop when taking input without words: "exit" etc
问题
我必须一直获取输入直到EOF(空格或行末)。例如:
1
2
3
4
// 结束输入
我正在使用 Scanner
来获取输入。我尝试了这个解决方案。
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
ArrayList<String> password = new ArrayList<>();
String input;
while (!(input = sc.nextLine()).equals("")) {
password.add(input);
}
boolean[] lengthValidation = lengthCheck(password);
boolean[] passwordContainCheck = containCheck(password);
print(lengthValidation, passwordContainCheck);
}
我不能使用这个,因为uri不接受它的变体回答。
while(true) {
String input = sc.nextLine();
if (input.equals(""))
break;
}
英文:
I have to take inputs until EOF (white space or end of line). For example:
1
2
3
4
// end of taking input
I'm using Scanner
to take inputs. I tried this solution.
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
ArrayList<String> password = new ArrayList<>();
String input;
while (!(input = sc.nextLine()).equals("")) {
password.add(input);
}
boolean[] lengthValidation = lengthCheck(password);
boolean[] passwordContainCheck = containCheck(password);
print(lengthValidation, passwordContainCheck);
}
I can't use this, due to uri does not take it variant of answer
while(true) {
String input = sc.nextLine();
if (input.equals(""))
break;
}
答案1
得分: 1
我必须持续获取输入,直到遇到 EOF(空白或行末)。
尝试以下方法。每行输入一次。以空行结束。您可以将它们存储在一个列表中,在循环结束时使用。
Scanner input = new Scanner(System.in);
String v;
List<String> items = new ArrayList<>();
while (!(v = input.nextLine()).isBlank()) {
items.add(v);
}
System.out.println(items);
英文:
>I have to take inputs until EOF (white space or end of line).
Try the following. One entry per line. Terminate with an empty line. You can store them in a list and use them when the loop terminates.
Scanner input = new Scanner(System.in);
String v;
List<String> items = new ArrayList<>();
while (!(v = input.nextLine()).isBlank()) {
items.add(v);
}
System.out.println(items);
</details>
# 答案2
**得分**: 0
也许这就是你正在寻找的内容,Scanner会接收输入,直到遇到空行,然后循环停止。
```java
Scanner sc = new Scanner(System.in);
ArrayList<String> password = new ArrayList<>();
String input;
while (true) {
input = sc.nextLine();
if (input.equals("")) break;
password.add(input);
}
英文:
Maybe this is what you are looking for, Scanner take input until it meets an empty line, then the loop stop
Scanner sc = new Scanner(System.in);
ArrayList<String> password = new ArrayList<>();
String input;
while (true) {
input = sc.nextLine();
if (input.equals("")) break;
password.add(input);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论