我的循环出了什么问题?不断收到 NoSuchElementException

发布于 2024-10-01 19:38:40 字数 860 浏览 0 评论 0原文

我在 maze[r][c]=scan.next(); 行不断收到 NoSuchElement 异常。我该如何解决这个问题?

  try {
        Scanner scan = new Scanner(f);
        String infoLine = scan.nextLine();
        int rows=0;
        int columns=0;
        for(int i = 0; i<infoLine.length();i++){
            if(Character.isDigit(infoLine.charAt(i))==true){
                rows = (int)infoLine.charAt(i);
                columns = (int)infoLine.charAt(i+1);
                break;
            }
        }

        String [][] maze = new String[rows][columns];
        int r = 0;
        while(scan.hasNextLine()==true && r<rows){
            for(int c = 0; c<columns;c++){
                maze[r][c]=scan.next();
            }
            r++;
        }
        return maze;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

I keep getting a NoSuchElement Exception at the line maze[r][c]=scan.next();. How can I resolve that?

  try {
        Scanner scan = new Scanner(f);
        String infoLine = scan.nextLine();
        int rows=0;
        int columns=0;
        for(int i = 0; i<infoLine.length();i++){
            if(Character.isDigit(infoLine.charAt(i))==true){
                rows = (int)infoLine.charAt(i);
                columns = (int)infoLine.charAt(i+1);
                break;
            }
        }

        String [][] maze = new String[rows][columns];
        int r = 0;
        while(scan.hasNextLine()==true && r<rows){
            for(int c = 0; c<columns;c++){
                maze[r][c]=scan.next();
            }
            r++;
        }
        return maze;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-10-08 19:38:40

查看代码的这一部分:

    while(scan.hasNextLine()==true && r<rows){  // 1
        for(int c = 0; c<columns;c++){          // 2
            maze[r][c]=scan.next();             // 3
        }                                       // 4
        r++;                                    // 5
    }                                           // 6

在第 1 行中,您正在检查以确保扫描还有另一行可用。但在第 3 行中,您在 2:4 循环内读到了该行。因此,如果有多于 1 列,您将多次请求下一次扫描 - 并且您只检查是否有下一行。因此,在第二列中,如果您处于扫描末尾,您会尝试从扫描中读取数据,即使它已用完。

试试这个:

try {
    Scanner scan = new Scanner(f);
    String infoLine = scan.nextLine();
    int rows = 0;
    int columns = 0;
    for (int i = 0; i < infoLine.length();i++) {
        if (Character.isDigit(infoLine.charAt(i))) {
            rows = Character.digit(infoLine.charAt(i), 10);
            columns = Character.digit(infoLine.charAt(i + 1), 10);
            break;
        }
    }

    String [][] maze = new String[rows][columns];
    int r = 0;
    while(scan.hasNextLine() && r < rows) {
        int c = 0;
        while(scan.hasNextLine() && c < columns) {
            maze[r][c]=scan.next();
            c++
        }
        r++;
    }
    return maze;
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Look at this part of your code:

    while(scan.hasNextLine()==true && r<rows){  // 1
        for(int c = 0; c<columns;c++){          // 2
            maze[r][c]=scan.next();             // 3
        }                                       // 4
        r++;                                    // 5
    }                                           // 6

In line 1 you are checking to make sure that scan has another line available. But in line 3, you read that line - inside the 2:4 loop. So if there are more than 1 columns, you will be asking for the next scan more than once - and you only checked to see if there was one next line. So on the second column, if you're at the end of scan, you try to read from scan even though it's run out.

Try this:

try {
    Scanner scan = new Scanner(f);
    String infoLine = scan.nextLine();
    int rows = 0;
    int columns = 0;
    for (int i = 0; i < infoLine.length();i++) {
        if (Character.isDigit(infoLine.charAt(i))) {
            rows = Character.digit(infoLine.charAt(i), 10);
            columns = Character.digit(infoLine.charAt(i + 1), 10);
            break;
        }
    }

    String [][] maze = new String[rows][columns];
    int r = 0;
    while(scan.hasNextLine() && r < rows) {
        int c = 0;
        while(scan.hasNextLine() && c < columns) {
            maze[r][c]=scan.next();
            c++
        }
        r++;
    }
    return maze;
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文