Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3345896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:14:22+00:00 2026-05-18T01:14:22+00:00

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

  • 0

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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:14:23+00:00Added an answer on May 18, 2026 at 1:14 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I keep getting a exception when I try to FTP to my Win 2008
I keep getting a warning that the UIView may not respond to selectedSegmentIndex. I
I keep getting compiler errors when I try to access flashVars in an AS3
I keep getting the following error: * #1005 - Can't create table 'classorganizer.turma' (errno:
I keep getting this error when I try to build my solution. I've tried
I keep getting Line 43: public override void Execute() { Line 44: WriteLiteral(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ +
I keep getting the same error when I try to set an attribute but
I keep getting this error here: SEVERE: java.lang.NumberFormatException: For input string: at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at
I keep getting this error when I try to build my project which uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.