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

  • Home
  • SEARCH
  • 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 8673667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:31:02+00:00 2026-06-12T19:31:02+00:00

Earlier today I asked a question on how to place images in sequence as

  • 0

Earlier today I asked a question on how to place images in sequence as part of a whole picture, so that I could generate 2D maps like you see in the old Pokemon games. Everything is tile-based.

I’ve come to the point where I generate the map itself, but it comes out a little funny.

I’ve tracked the problem to when I fill a HashMap with Tile Objects. A Tile Object holds a Name and a BufferedImage. What seems to be the problem is how the program reads the files from the folder filled with tiles (png files) and place them in the HashMap.

Here is the code for when I place the Tile Objects in the HashMap:

private static HashMap<Integer, Tile> getTiles(String path) throws IOException {
    HashMap<Integer, Tile> temp = new HashMap<>();
    File folder = new File(path);
    Tile tile;
    int counter = 0;
    for(File file : folder.listFiles()) {
        if(file.isFile()) {
            try {
                if(file.getCanonicalPath().endsWith(".png")) {
                    tile = new Tile(file.getName(),ImageIO.read(file));
                    temp.put(counter, tile);
                    counter++;
                }
            } catch(IOException ioe) {
                System.out.println("Picture number " + counter + " could "
                        + "not be saved in the hashmap: \n"
                        + ioe.getMessage());
                continue;
            }
        }
    }
    return temp;
}

And here is the Output when I do a toString() on the HashMap:

Tiles: (0,0.png)(1,1.png)(2,10.png)(3,100.png)....

Notice how after tile 1.png it starts counting 10, then 100, then 101, then 102…then when it reaches 112 it starts counting like this:

(112,2.png)(113,20.png)(114,200.png)(115,201.png)...

There is a total of 220 Tiles in this folder, (counting tile 0.png).
I clearly see what the problem is, but not how I fix it.

  • 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-06-12T19:31:04+00:00Added an answer on June 12, 2026 at 7:31 pm

    I would guess that folder.listFiles() doesn’t return the files in order. You could put these into a List and then sort it, but this isn’t a great approach. If image 50.png were removed and someone added 100000.png then all the images after 49 would be shifted over by one. This could easily go undetected.

    The better approach is to split the filename by the period and parse it to an int.

    Here is how to split the filename:

    String filename = file.getName();
    String[] parts = filename.split("\\.");
    counter = Integer.parseInt(parts[0]);
    

    With this approach you no longer have to increment the counter. It also won’t improperly number a tile if the image is missing.

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

Sidebar

Related Questions

I asked a question earlier today about log4j and was told that it is
I asked a similar question earlier today, but now I am told that the
I asked a question here earlier today and got that fixed, but now I
I asked this question in the DataDynamics forum earlier today. I thought that maybe
I asked a question earlier today ( Methods of Managing Source Code ) as
I asked a question earlier today here . However, what I neglected to check
I just asked a question earlier today because I wanted to run an executable
Earlier today, I asked a question about the way Python handles certain kinds of
I asked another version of this question on the gamedev.SE site earlier today but
earlier today I asked this question . So since moq creates it's own class

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.