英文:
How to stop reading from a scanner if the user inputs a specific keyword?
问题
我需要能够在控制台输入随机数量的整数,然后在完成时输入特殊字符比如Q。然而,我不确定如何验证输入是否为整数。
用户的目标是输入x个整数,然后将这些整数从客户端发送到服务器,服务器再通过一个简单的等式返回结果。我计划逐一发送这些整数,因为可以输入任意数量的整数。
我尝试过几种不同的方法。我尝试过使用hasNextInt
方法。我尝试过使用nextLine
,然后将每个输入添加到一个ArrayList
,然后解析输入。
List<String> list = new ArrayList<>();
String line;
while (!(line = scanner.nextLine()).equals("Q")) {
list.add(line);
}
list.forEach(s -> os.write(parseInt(s)));
这是我最初的另一个循环,它可以很好地验证输入,但是我不确定如何在完成时退出循环。
while (x < 4) {
System.out.print("Enter a value: ");
while (!scanner.hasNextInt()) {
System.out.print("Invalid input: Integer Required (Try again):");
}
os.write(scanner.nextInt());
x++;
}
任何帮助都将不胜感激。谢谢。
英文:
I need to be able to enter a random number of integers into the console and then enter a special character such as Q when I am finished. However, I am not sure how to validate if the input is an int or not.
The point is for the user to enter x amount of ints, which get sent from client to server and the server returns the result from a simple equation. I plan on sending them one at a time as it can be any amount of ints entered.
I have tried several different ways. I have tried using hasNextInt. I tried nextLine then added each input to an ArrayList then parsed the input.
List<String> list = new ArrayList<>();
String line;
while (!(line = scanner.nextLine()).equals("Q")) {
list.add(line);
}
list.forEach(s -> os.write(parseInt(s)));
This is another loop I had originally which validates the input fine, but i'm not sure how to exit the loop when done.
while (x < 4) {
System.out.print("Enter a value: ");
while (!scanner.hasNextInt()) {
System.out.print("Invalid input: Integer Required (Try again):");
}
os.write(scanner.nextInt());
x++;
}
Any help would be appreciated. Thanks
答案1
得分: 2
这里是代码翻译结果:
这是代码部分,已经翻译完成。
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.equals("Q")) {
scanner.close();
break;
}
try {
int val = Integer.parseInt(line);
list.add(val);
} catch (NumberFormatException e) {
System.err.println("请输入一个数字或者 Q 退出。");
}
}
英文:
Here you go:
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.equals("Q")) {
scanner.close();
break;
}
try {
int val = Integer.parseInt(line);
list.add(val);
} catch (NumberFormatException e) {
System.err.println("Please enter a number or Q to exit.");
}
}
答案2
得分: 2
按照以下方式执行:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
String input = "";
while (true) {
System.out.print("Enter an integer (Q to quit): ");
input = in.nextLine();
if (input.equalsIgnoreCase("Q")) {
break;
}
if (!input.matches("\\d+")) {
System.out.println("This is an invalid entry. Please try again");
} else {
list.add(Integer.parseInt(input));
}
}
System.out.println(list);
}
}
一个示例运行:
Enter an integer (Q to quit): a
This is an invalid entry. Please try again
Enter an integer (Q to quit): 10
Enter an integer (Q to quit): b
This is an invalid entry. Please try again
Enter an integer (Q to quit): 20
Enter an integer (Q to quit): 10.5
This is an invalid entry. Please try again
Enter an integer (Q to quit): 30
Enter an integer (Q to quit): q
[10, 20, 30]
注:
while(true)
创建一个无限循环。\\d+
仅允许数字,即只允许整数。break
使循环中断。
英文:
Do it as follows:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
String input = "";
while (true) {
System.out.print("Enter an integer (Q to quit): ");
input = in.nextLine();
if (input.equalsIgnoreCase("Q")) {
break;
}
if (!input.matches("\\d+")) {
System.out.println("This is an invalid entry. Please try again");
} else {
list.add(Integer.parseInt(input));
}
}
System.out.println(list);
}
}
A sample run:
Enter an integer (Q to quit): a
This is an invalid entry. Please try again
Enter an integer (Q to quit): 10
Enter an integer (Q to quit): b
This is an invalid entry. Please try again
Enter an integer (Q to quit): 20
Enter an integer (Q to quit): 10.5
This is an invalid entry. Please try again
Enter an integer (Q to quit): 30
Enter an integer (Q to quit): q
[10, 20, 30]
Notes:
while(true)
creates an infinite loop.\\d+
allows only digits i.e. only an integer number.break
causes the loop to break.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论