-1

I´m implementing a Sokoban game in Java. I read a game field (Spielfeld) from a .txt file. Now I´m trying to create a char Array after I get the dimensions from the .txt file.

The problem is that I get the following error when compiling:

Pflichtaufgabe24.java:151: error: illegal start of expression static char[][] spielfeld = new char[arraylaenge][arraybreite];

Here is my code so far:

//author: Safwen Ben Said

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Pflichtaufgabe24 {
//C:\Users\safwen\Desktop\Sokoban
private static final String FILENAME      = "C:\\Users\\safwen\\Desktop\\Sokoban\\map.txt";
static char[][] safwensspielfeld = new char[4][8]; // Array 2-dimentionnel déclaré mais non initialisé



    static int aktuelles_x = 0;
    static int aktuelles_y = 0;

    static int arraylaenge = 0;
static int arraybreite = 0;

    public static void spielfeldausgeben(char[][] spielfeld, int anzahlzeilen, int anzahlspalten) {

        for (int k = 0; k < anzahlzeilen; k++) {
            for (int j = 0; j < anzahlspalten; j++) {
                if (j == 0) {
                    System.out.println(' ');
                }

                System.out.print(spielfeld[k][j]);

            }
        }
    }

    public static void move_up() {
       System.out.println(' ');
        if (aktuelles_x != 0) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_x = aktuelles_x - 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();

    }

    public static void move_down() {
        System.out.println(' ');
        if (aktuelles_x != 3) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_x = aktuelles_x + 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();
    }

    public static void move_right() {
        System.out.println(' ');
        if (aktuelles_y != 7) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_y = aktuelles_y + 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();
    }

    public static void move_left() {
        System.out.println(' ');
        if (aktuelles_y != 0) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_y = aktuelles_y - 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();

    }

    public static void exit() {
        System.out.println(' ');
        System.out.println("das Spiel wird auf Ihren Wunsch beendet.");
    }

    public static void main(String[] args) {
        Scanner safwens_scanner = new Scanner(System.in);
        String eingabe;


        BufferedReader br = null;
        FileReader fr = null;

            BufferedReader br2 = null;
        FileReader fr2 = null;

        try {

        fr = new FileReader(FILENAME);
        br = new BufferedReader(fr);

            String sCurrentLine;
            int iCurrentLine = 0;

            br = new BufferedReader(new FileReader(FILENAME));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                iCurrentLine ++;
                if (sCurrentLine.length() > arraybreite){
                    arraybreite = sCurrentLine.length();
                }
                System.out.println(arraybreite); //Safwen´s  little check while programming (not part of the final solution)
                arraylaenge++;




            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }
    static char[][] spielfeld = new char[arraylaenge][arraybreite];

    try {

        fr2 = new FileReader(FILENAME);
        br2 = new BufferedReader(fr);

        String sCurrentLine2;
        int iCurrentLine2 = 0;

        br2 = new BufferedReader(new FileReader(FILENAME));

    while ((sCurrentLine2 = br2.readLine()) != null) {



             for (int z = 0; z < sCurrentLine2.length(); z++){ 

            spielfeld[iCurrentLine2][z] = sCurrentLine2.charAt(z);
             }



        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }
    spielfeldausgeben(spielfeld, arraylaenge, arraybreite);

    }

 }

I would be grateful for any hints or help.

PS: Here is my map.txt file

#####
#@$.#
#####

PS 2:

 # : wall @:  player
 + : player on target field 
 $ : box
 * : box on target field 
 . : target field 
 ´´ : field
TT.
  • 15,774
  • 6
  • 47
  • 88
  • Variables declared inside a method cannot be `static`. If this was meant to be a member variable, declare it outside of a method. If this was meant to be a local variable, remove the `static` keyword. Improve the indentation of your code, so that it's easier to see the structure. – Jesper Dec 03 '16 at 10:44
  • Thanks for your help. I get the following new error after removing `static` – Safwen Ben Said Dec 03 '16 at 10:50
  • `Pflichtaufgabe24.java:48: error: method spielfeldausgeben in class Pflichtaufgabe24 cannot be applied to given types; spielfeldausgeben(); ^ required: char[][],int,int found: no arguments reason: actual and formal argument lists differ in length` 4 times – Safwen Ben Said Dec 03 '16 at 10:51
  • Use an IDE. Don't just compile your app and think it'll work – OneCricketeer Dec 03 '16 at 10:56
  • `public static void spielfeldausgeben(char[][] spielfeld, int anzahlzeilen, int anzahlspalten) {`... This has three parameters. So, how do you expect this to work? `spielfeldausgeben();` – OneCricketeer Dec 03 '16 at 10:58
  • I'm really very thankful guys. Jesper: Your answer is the solution. cricket_007: I changed the method but kept on calling it without Arguments. Your hint brought my Programm to work the way it should. – Safwen Ben Said Dec 03 '16 at 12:56

1 Answers1

1

Variables declared inside a method cannot be static.