-1

Good evening,

for hours I've been searching for a solution for this problem without success so I figured I'd ask a question here! I have a question regarding reading characters / lines out of a textfile. I have been able to implement a function, which reads lines out of a file. I am using Greenfoot (Java) to create a game which uses 32x32 blocks. I'd like to generate a world out of that textfile by getting the x- / y-coordinate of each "block" / "character" and placing it in the world. Would it be easier to use an array? My current code looks like this but I cannot figure out, how to get the coordinates. Could it work by using the hashcode it returns?

    public void readFile(String filename) throws IOException
{      
    String s;
    int x = 0;
    int y = 0;

    // Create BufferedReader and FileReader
    BufferedReader r = new BufferedReader(new FileReader(filename));

    // Try-catch block as exception handler 
    try {
        while((s = r.readLine()) != null) {

            // Create the blocks
            GrassBlock A = new GrassBlock();

            // Place them in the world
            addObject(A, x, y);

            // Test to see, if the blocks get recognised
            System.out.println(A);

            DirtBlock B = new DirtBlock();
            System.out.println(B);
            addObject(B, x, y);
        }            
    } catch (IOException e) {
        System.out.println("Fehler beim Öffnen der Datei");
    } finally {

    }
}

My file looks somewhat like this:

0000000000
0000000000
AAAAAA00AA
BBBBBB00BB

I see that I have assigned the value "0" to both x and y so of course it can't work like this but how can I get that position? Right now the function is able to read the lines, generate the blocks at (0, 0) and showing the blocks in the console with a hashcode.

P.S Sorry if I have used the wrong term for some things, I am relatively new to programming!

Thank you, Julian

  • Hints: 1. what is the format of your file, how are the coordinates/block type saved in the file? 2. generally it is more helpful to print the StackTrace (`e.printStackTrace()`) in the catch block, or at least the message and the class of the Exception – user85421 May 11 '17 at 21:37
  • Maybe the line number is the _y_ coordinate and the position of each character in a line the _x_? – Kevin Anderson May 12 '17 at 01:53
  • @CarlosHeuberger Thank you! It is just a simple .txt file with 0, A's, B's etc. I'll change that, thanks! – Julian Friedrich May 12 '17 at 09:42
  • @KevinAnderson Thank you, can you just ask for the line number though or would you have to read out every x/y manually? – Julian Friedrich May 12 '17 at 09:43
  • With a `BufferedReader` you'd have to keep a count of the lines yourself. You could just add `++y;` at the bottom of your `while` loop. Or have a look at [LineNumberReader](https://docs.oracle.com/javase/7/docs/api/java/io/LineNumberReader.html). – Kevin Anderson May 12 '17 at 10:18
  • @KevinAnderson Thank you! But is it really that easy to add a "++y"? It would increase y every time we have read a line right? So what about the x-coordinates? – Julian Friedrich May 12 '17 at 15:00

1 Answers1

0

Just the very basic, without changing question code too much, not complete

  1. determining the line number y:

    int y = 0;
    while ((s = ...) != null) {
        // do something with s
        y += 1;  // or y++
    }
    
  2. similar for character position x inside the line and using charAt to retrieve the character:

    while ((s = ...) != null)  {
        int x = 0;
        while (x < s.length()) {
            char ch = s.charAt(x);
            // do something with ch, x, y - test for 0, A or B and add block
            x += 1;  // or x++
        }
        y += 1;  // or y++
    }
    

Notes: x is declared inside the outer (first) loop since it is not needed elsewhere; x is set to zero before the inner loop since it is the start of a new line; a for loop could be used instead of while - mostly better when using counters (like x)

user85421
  • 28,957
  • 10
  • 64
  • 87
  • Thank you for your help but for some reason I can't quite figure it out. What do you mean by "do something with"? I'm still quite flawed at understanding programming and I do not know what I would have to do. So I'm telling the program to read the first line while there's something there. Then I have to use the string to do something? For the x-part I read the character, set x to 0 again and increase x until I have reached the end of the file. Then I increase y to go to the next line? Do I have to put both while loops in a try-catch block? – Julian Friedrich May 12 '17 at 14:17