You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java程序使用Scanner读取输入时抛出NoSuchElementException求助

解决Scanner读取输入时的NoSuchElementException异常

问题详情

运行代码时抛出以下异常:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at Solution.main(Solution.java:11)

输入格式要求:第一行输入测试用例数量,接下来对应行数为正则表达式模式字符串。原代码如下:

import java.util.Scanner;
import java.util.regex.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
        while (testCases > 0) {
            String pattern = in.nextLine();
            char[] p = pattern.toCharArray();
            Stack<Character> stack = new Stack<>();
            for (int i = 0; i < p.length; i++) {
                if (p[i] == '(' || p[i] == '[' || p[i] == '{') {
                    stack.push(p[i]);
                } else if (p[i] == ')' || p[i] == ']' || p[i] == '}') {
                    if (!stack.isEmpty() && stack.peek() == '{' && p[i] == '}') {
                        stack.pop();
                    } else if (!stack.isEmpty() && stack.peek() == '[' && p[i] == ']') {
                        stack.pop();
                    } else if (!stack.isEmpty() && stack.peek() == '(' && p[i] == ')') {
                        stack.pop();
                    }
                }
            }
        }
    }
}

异常原因

  1. 无限循环耗尽输入:代码中while (testCases > 0)循环未在每次迭代后递减testCases值,循环会持续执行,不断调用in.nextLine()读取输入。当所有输入行被读完后,继续调用nextLine()就会抛出NoSuchElementException。
  2. 未检查输入可用性:直接调用nextLine()而未提前判断输入流中是否还有可用行,若输入行数少于指定的测试用例数量,也会触发异常。

解决方案

  • 在while循环末尾添加testCases--;,让循环能正常终止。
  • 读取每一行前,用in.hasNextLine()检查是否还有输入行,避免无输入时调用nextLine()。

修改后的代码

import java.util.Scanner;
import java.util.Stack;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
        
        while (testCases > 0) {
            // 先检查是否还有输入行
            if (in.hasNextLine()) {
                String pattern = in.nextLine();
                char[] p = pattern.toCharArray();
                Stack<Character> stack = new Stack<>();
                
                for (char c : p) {
                    if (c == '(' || c == '[' || c == '{') {
                        stack.push(c);
                    } else if (c == ')' || c == ']' || c == '}') {
                        if (!stack.isEmpty()) {
                            char top = stack.peek();
                            if ((top == '{' && c == '}') || 
                                (top == '[' && c == ']') || 
                                (top == '(' && c == ')')) {
                                stack.pop();
                            }
                        }
                    }
                }
                // 可根据需求添加结果输出,比如:
                // System.out.println(stack.isEmpty() ? "Valid" : "Invalid");
            }
            // 递减测试用例计数
            testCases--;
        }
        in.close(); // 关闭Scanner释放资源
    }
}

内容的提问来源于stack exchange,提问作者sachin samuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 08:30:19