-1
\$\begingroup\$

Ok, so I have a text file filled with 0s and 1s. When there are 1s, I want the code to add a block to the block array. For some reason it is not reading it right at all.

World.java:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;

class World {
    Image ground;
    Block[] blocks;
    int mapWidth = 40;
    int mapHeight = 22;

    void setWorld(String map) {
        try { 
            BufferedReader br = new BufferedReader(new FileReader(map));
            String line;
            int enc = -1;
            int x = 0;
            int y = 0;
            while ((line = br.readLine()) != null) {
                String values[] = line.split(" ");
                for (String str : values) {
                    int str_int = Integer.parseInt(str);
                    if (str_int == 1) {
                        enc++;
                        System.out.println("x: " + x + ", y: " + y);
                        //blocks[enc] = new Block(x * 32, y * 32, 32, 32, 1);
                    }
                    y++;
                }
                x++;               
            }

        } catch (Exception e) { e.printStackTrace(); }
        System.out.println("hi");
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Maybe swap where you increment x and y? \$\endgroup\$ Commented Jan 22, 2017 at 17:41
  • \$\begingroup\$ Debugging requires figuring out how & why actual results aren't matching expected results. You gave the code, but without knowing the input, the expected output & the actual output, we don't have much to go on. What's the simplest, smallest test case that fails? What did you expect instead? \$\endgroup\$ Commented Jan 22, 2017 at 21:37
  • \$\begingroup\$ Your telling us "it does not read right", but your not telling us how it is reading. Your describing a problem by telling us that it exists, but your not telling us what it is. \$\endgroup\$ Commented Jan 23, 2017 at 1:13

1 Answer 1

-1
\$\begingroup\$

I fixed it.

I swapped the x and the y increments, and set x to 0 when I incremented y. I also use a list to store the blocks, instead of an array.

\$\endgroup\$
1
  • \$\begingroup\$ Make sure you come back to accept the answer, so others know this is solved \$\endgroup\$ Commented Jan 23, 2017 at 1:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.