.txt--> CharArray

HunterCD

Mitglied
Hallo alle zusammen,
ich will einfach nur den gesamten Inhalt einer Datei in einem Char-Array speichern.
hier mein code:
Code:
public static void main(String[] unused) throws IOException {
        BufferedReader br;
        String line;
        FileReader fr;

        File asasa= new File("asasa");
        br = new BufferedReader(new FileReader(asas));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        // player position (horizontal)
        int xPlayer = 0;
        // player position (vertical)
        int yPlayer = 0;

        // create room
        char[][] room = new char[20][20];
       

        // initialize room with dots
        for (int x = 0; x < room.length; x++) {
            for (int y = 0; y < room[x].length; y++) {
                room[x][y] = '.';
            }
        }

        // set player start position in top left corner (origin)
        room[xPlayer][yPlayer] = 'P';

        // create new Scanner that reads from console
        Scanner scan = new Scanner(System.in);

        // flag if we quit the program
        boolean run = true;

        do {
            // print room row for row (thats why we start with y instead of
            // x)
            for (int y = 0; y < room[0].length; y++) {
                for (int x = 0; x < room.length; x++) {
                    System.out.print(room[x][y]);
                }
                System.out.println();
            }

            System.out.println("Do you want to go up, down, left, right or exit the program?");

            // check which command was chosen and execute it
            switch (scan.next()) {
            case "w":
            case "up":
                if (yPlayer > 0) {
                    room[xPlayer][yPlayer] = '.'; // set dot on old player
                                                    // position
                    yPlayer--; // move player to new place
                    room[xPlayer][yPlayer] = 'P'; // set new player position
                } else {
                    System.out.println("You can not go there!");
                }
                break;
            case "s":
            case "down":
                if (yPlayer < room[0].length - 1) {
                    room[xPlayer][yPlayer] = '.';
                    yPlayer++;
                    room[xPlayer][yPlayer] = 'P';
                } else {
                    System.out.println("You can not go there!");
                }
                break;
            case "a":
            case "left":
                if (xPlayer > 0) {
                    room[xPlayer][yPlayer] = '.';
                    xPlayer--;
                    room[xPlayer][yPlayer] = 'P';
                } else {
                    System.out.println("You can not go there!");
                }
                break;
            case "d":
            case "right":
                if (xPlayer < room.length - 1) {
                    room[xPlayer][yPlayer] = '.';
                    xPlayer++;
                    room[xPlayer][yPlayer] = 'P';
                } else {
                    System.out.println("You can not go there!");
                }
                break;
            case "exit":
                run = false;
                break;
            // if the user input is not one of our commands print help
            default:
                System.out.println(
                        "Command unknown! Please type up, down, left or right to move or exit to quit this program");
            }
        } while (run);
        System.out.println("Goodbye");

    }
}
 

Robat

Top Contributor
Verstehe dein Problem nicht ganz.

Hier liest du die Datei doch ein, dann schreib doch auch hier die Daten in dein Array.

while ((line = br.readLine()) != null) {
System.out.println(line);
}

Das hier
char[][] room = new char[20][20];
musst du dann eben noch mit bei deinen anderen Variablen
BufferedReader br;
String line;
FileReader fr;
deklarieren damit es klappt.

Gruß
Robert
 

Ähnliche Java Themen


Oben