Java 如果用户输入特定关键字,如何停止从扫描仪读取?

Java 如果用户输入特定关键字,如何停止从扫描仪读取?,java,java.util.scanner,Java,Java.util.scanner,我需要能够在控制台中输入一个随机数目的整数,然后在完成时输入一个特殊字符,如Q。但是,我不知道如何验证输入是否为int 关键是用户要输入x个整数,这些整数从客户端发送到服务器,服务器通过一个简单的等式返回结果。我计划一次发送一个,因为可以输入任意数量的整数 我试过几种不同的方法。我试过使用hasNextInt。我尝试了nextLine,然后将每个输入添加到ArrayList,然后解析输入 List<String> list = new ArrayList<>(); Str

我需要能够在控制台中输入一个随机数目的整数,然后在完成时输入一个特殊字符,如Q。但是,我不知道如何验证输入是否为int

关键是用户要输入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)));
List List=new ArrayList();
弦线;
而(!(line=scanner.nextLine()).equals(“Q”)){
列表。添加(行);
}
list.forEach->os.write(parseInt);
这是我最初使用的另一个循环,它可以很好地验证输入,但我不确定完成后如何退出循环

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++;
}
while(x<4){
System.out.print(“输入值:”);
而(!scanner.hasnetint()){
System.out.print(“无效输入:需要整数(重试):”;
}
write(scanner.nextInt());
x++;
}
任何帮助都将不胜感激。谢谢

给你:

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.");
    }
}
Scanner Scanner=新的扫描仪(System.in);
列表=新的ArrayList();
while(scanner.hasNext()){
字符串行=scanner.nextLine();
如果(直线等于(“Q”)){
scanner.close();
打破
}
试一试{
int val=Integer.parseInt(行);
列表。添加(val);
}捕获(数字格式){
System.err.println(“请输入一个数字或Q以退出”);
}
}
给你:

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.");
    }
}
Scanner Scanner=新的扫描仪(System.in);
列表=新的ArrayList();
while(scanner.hasNext()){
字符串行=scanner.nextLine();
如果(直线等于(“Q”)){
scanner.close();
打破
}
试一试{
int val=Integer.parseInt(行);
列表。添加(val);
}捕获(数字格式){
System.err.println(“请输入一个数字或Q以退出”);
}
}
按以下步骤操作:

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
    导致循环中断
  • 如有任何疑问/问题,请随时发表意见。

    请按以下步骤进行:

    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
    导致循环中断

  • 如有任何疑问/问题,请随时发表评论。

    谢谢。花了太长时间想弄明白,效果很好。谢谢。花了太长时间想弄明白,效果很好。