如何在用户输入特定关键词后停止从扫描器读取?

huangapple go评论87阅读模式
英文:

How to stop reading from a scanner if the user inputs a specific keyword?

问题

我需要能够在控制台输入随机数量的整数,然后在完成时输入特殊字符比如Q。然而,我不确定如何验证输入是否为整数。

用户的目标是输入x个整数,然后将这些整数从客户端发送到服务器,服务器再通过一个简单的等式返回结果。我计划逐一发送这些整数,因为可以输入任意数量的整数。

我尝试过几种不同的方法。我尝试过使用hasNextInt方法。我尝试过使用nextLine,然后将每个输入添加到一个ArrayList,然后解析输入。

  1. List<String> list = new ArrayList<>();
  2. String line;
  3. while (!(line = scanner.nextLine()).equals("Q")) {
  4. list.add(line);
  5. }
  6. list.forEach(s -> os.write(parseInt(s)));

这是我最初的另一个循环,它可以很好地验证输入,但是我不确定如何在完成时退出循环。

  1. while (x < 4) {
  2. System.out.print("Enter a value: ");
  3. while (!scanner.hasNextInt()) {
  4. System.out.print("Invalid input: Integer Required (Try again):");
  5. }
  6. os.write(scanner.nextInt());
  7. x++;
  8. }

任何帮助都将不胜感激。谢谢。

英文:

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.

  1. List&lt;String&gt; list = new ArrayList&lt;&gt;();
  2. String line;
  3. while (!(line = scanner.nextLine()).equals(&quot;Q&quot;)) {
  4. list.add(line);
  5. }
  6. list.forEach(s -&gt; 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.

  1. while (x &lt; 4) {
  2. System.out.print(&quot;Enter a value: &quot;);
  3. while (!scanner.hasNextInt()) {
  4. System.out.print(&quot;Invalid input: Integer Required (Try again):&quot;);
  5. }
  6. os.write(scanner.nextInt());
  7. x++;
  8. }

Any help would be appreciated. Thanks

答案1

得分: 2

这里是代码翻译结果:

  1. 这是代码部分已经翻译完成
  2. Scanner scanner = new Scanner(System.in);
  3. List<Integer> list = new ArrayList<Integer>();
  4. while (scanner.hasNext()) {
  5. String line = scanner.nextLine();
  6. if (line.equals("Q")) {
  7. scanner.close();
  8. break;
  9. }
  10. try {
  11. int val = Integer.parseInt(line);
  12. list.add(val);
  13. } catch (NumberFormatException e) {
  14. System.err.println("请输入一个数字或者 Q 退出。");
  15. }
  16. }
英文:

Here you go:

  1. Scanner scanner = new Scanner(System.in);
  2. List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  3. while (scanner.hasNext()) {
  4. String line = scanner.nextLine();
  5. if (line.equals(&quot;Q&quot;)) {
  6. scanner.close();
  7. break;
  8. }
  9. try {
  10. int val = Integer.parseInt(line);
  11. list.add(val);
  12. } catch (NumberFormatException e) {
  13. System.err.println(&quot;Please enter a number or Q to exit.&quot;);
  14. }
  15. }

答案2

得分: 2

按照以下方式执行:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. List<Integer> list = new ArrayList<Integer>();
  8. String input = "";
  9. while (true) {
  10. System.out.print("Enter an integer (Q to quit): ");
  11. input = in.nextLine();
  12. if (input.equalsIgnoreCase("Q")) {
  13. break;
  14. }
  15. if (!input.matches("\\d+")) {
  16. System.out.println("This is an invalid entry. Please try again");
  17. } else {
  18. list.add(Integer.parseInt(input));
  19. }
  20. }
  21. System.out.println(list);
  22. }
  23. }

一个示例运行:

  1. Enter an integer (Q to quit): a
  2. This is an invalid entry. Please try again
  3. Enter an integer (Q to quit): 10
  4. Enter an integer (Q to quit): b
  5. This is an invalid entry. Please try again
  6. Enter an integer (Q to quit): 20
  7. Enter an integer (Q to quit): 10.5
  8. This is an invalid entry. Please try again
  9. Enter an integer (Q to quit): 30
  10. Enter an integer (Q to quit): q
  11. [10, 20, 30]

注:

  1. while(true) 创建一个无限循环。
  2. \\d+ 仅允许数字,即只允许整数。
  3. break 使循环中断。
英文:

Do it as follows:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  8. String input = &quot;&quot;;
  9. while (true) {
  10. System.out.print(&quot;Enter an integer (Q to quit): &quot;);
  11. input = in.nextLine();
  12. if (input.equalsIgnoreCase(&quot;Q&quot;)) {
  13. break;
  14. }
  15. if (!input.matches(&quot;\\d+&quot;)) {
  16. System.out.println(&quot;This is an invalid entry. Please try again&quot;);
  17. } else {
  18. list.add(Integer.parseInt(input));
  19. }
  20. }
  21. System.out.println(list);
  22. }
  23. }

A sample run:

  1. Enter an integer (Q to quit): a
  2. This is an invalid entry. Please try again
  3. Enter an integer (Q to quit): 10
  4. Enter an integer (Q to quit): b
  5. This is an invalid entry. Please try again
  6. Enter an integer (Q to quit): 20
  7. Enter an integer (Q to quit): 10.5
  8. This is an invalid entry. Please try again
  9. Enter an integer (Q to quit): 30
  10. Enter an integer (Q to quit): q
  11. [10, 20, 30]

Notes:

  1. while(true) creates an infinite loop.
  2. \\d+ allows only digits i.e. only an integer number.
  3. break causes the loop to break.

huangapple
  • 本文由 发表于 2020年4月8日 01:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61085897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定