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 4100566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:35:01+00:00 2026-05-20T20:35:01+00:00

When I say a grid, I mean a multidimensional array. I want this because

  • 0

When I say a grid, I mean a multidimensional array. I want this because I am making a 2D game and I want to be able to load levels from data text files. Lets say, for example, I have this 2D array level[3][3]. A simple 3×3 map. And I also have a text file that reads:

1 2 3 
4 5 6
7 8 9

In c++, I can simply do:

for (x=0; i<map_width; x++)
{
    for (y=0; y<map_height; y++)
    {
        fscanf(nameoffile, "%d", map[x][y]);
    }
}

And that would put all the contents of the text file accordingly into the array.
HOWEVER
I have no idea how to do this in java. Is there any sort of equivalent that will just place the data into the array accordingly? I already know about the scanner class, but I don’t know how to use it. I have searched google, to no avail. It doesn’t explain much.
Please help! Specifically, I want to know how to scan the file and put whatever int it reads IN THE APPROPRIATE PLACE in the array.

My current code is this, however, it throws a NoSuchElementException:

public void loadMap() {
    Scanner sc = null;
    try {
        sc = new Scanner(inputmap);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            map[x][y] = sc.nextInt();
        }
    }

Where inputmap is the file, map[][] is a grid of data for each of the tiles on the map and width and height are pre-specified in a constructor.

  • 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-20T20:35:02+00:00Added an answer on May 20, 2026 at 8:35 pm

    Your question is very unhelpful when it comes to how your text files will be actually formatted. For example,

    123 
    456
    789
    

    is very different from

    1 2 3
    4 5 6
    7 8 9
    

    and furthermore, you haven’t mentioned whether they are always ints, or

    1 2 3
    4 5 6
    a b c
    

    etc. If you gave us a precise description of exactly what goes in these text files we could help you more. The best I can do is show you how to use Scanner to input stuff in general:

    The for loop would look similar in Java, but you have to initialize a Scanner object.

    Scanner input = new Scanner(myFile); //whatever file is being read
    
    for (x=0; i<map_width; x++)
    {
        for (y=0; y<map_height; y++)
        {
            int nextTile = input.nextInt(); //reads the next int
            // or
            char nextTile = input.nextChar(); //reads the next char
        }
    }
    

    Beyond that, I would need to know more about what is actually inside these input files.

    EDIT:

    I copied your for loop directly from your code, but you may want to swap the inner and outer for loops. Shouldn’t the width be the inner parameter (reading left to right)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a new game that generates a huge grid (lets say 1000x1000). The
Say I have a Telerik MVC Grid, AJAX bound and I want to put
Lets say I have 2 columns in my data Grid: Column A: Selected, and
Let's say I have some grid that looks like this _ _ _ _
I have a grid in asp, with data keys say 'job_no' , I provided
Let’s say I have a data grid and there’s a method attached to clicking
There are various points in a Grid. Say (x,y) can be reached from (x-1,y+1)
i am using data grid view in C# winforms, lets say there are n
I have Textboxes in UI as inside a grid . I mean to say
(See this question and this question for background...) Given: grid = new Slick.Grid(#myGrid, data,

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.