Einlesen einer txt.datei und speichern in einem char[][]?

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Einlesen einer txt.datei und speichern in einem char[][]?

    Guten Abend,

    ich benötige Hilfe beim einlesen einer Textdatei mithilfe des BufferredReaders, welches im Folgeschritt mein CharArray überschreiben soll.
    Hier mein Quellqode : :)

    Java-Quellcode

    1. // needed to use Scanner for user input
    2. import java.util.Scanner;
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileReader;
    6. import java.io.IOException;
    7. /**
    8. * This class is the first part for the Sokoban game with a very simple rule
    9. * set.
    10. *
    11. * @author Jane Doe 1234567 Group 42h
    12. * @author John Doe 1234567 Group 42h
    13. */
    14. class SokobanTesto {
    15. /**
    16. * The Main method for the Sokoban game with contains all of the game logic
    17. *
    18. * @param unused
    19. * is unused
    20. */
    21. public static void main(String[] unused) throws IOException {
    22. BufferedReader br;
    23. String line;
    24. FileReader fr;
    25. File soki = new File("soki.txt");
    26. br = new BufferedReader(new FileReader(soki));
    27. while ((line = br.readLine()) != null) {
    28. System.out.println(line);
    29. }
    30. // player position (horizontal)
    31. int xPlayer = 0;
    32. // player position (vertical)
    33. int yPlayer = 0;
    34. // create room
    35. char[][] room = new char[20][20];
    36. // initialize room with dots
    37. for (int x = 0; x < room.length; x++) {
    38. for (int y = 0; y < room[x].length; y++) {
    39. room[x][y] = '.';
    40. }
    41. }
    42. // set player start position in top left corner (origin)
    43. room[xPlayer][yPlayer] = 'P';
    44. // create new Scanner that reads from console
    45. Scanner scan = new Scanner(System.in);
    46. // flag if we quit the program
    47. boolean run = true;
    48. do {
    49. // print room row for row (thats why we start with y instead of
    50. // x)
    51. for (int y = 0; y < room[0].length; y++) {
    52. for (int x = 0; x < room.length; x++) {
    53. System.out.print(room[x][y]);
    54. }
    55. System.out.println();
    56. }
    57. System.out.println("Do you want to go up, down, left, right or exit the program?");
    58. // check which command was chosen and execute it
    59. switch (scan.next()) {
    60. case "w":
    61. case "up":
    62. if (yPlayer > 0) {
    63. room[xPlayer][yPlayer] = '.'; // set dot on old player
    64. // position
    65. yPlayer--; // move player to new place
    66. room[xPlayer][yPlayer] = 'P'; // set new player position
    67. } else {
    68. System.out.println("You can not go there!");
    69. }
    70. break;
    71. case "s":
    72. case "down":
    73. if (yPlayer < room[0].length - 1) {
    74. room[xPlayer][yPlayer] = '.';
    75. yPlayer++;
    76. room[xPlayer][yPlayer] = 'P';
    77. } else {
    78. System.out.println("You can not go there!");
    79. }
    80. break;
    81. case "a":
    82. case "left":
    83. if (xPlayer > 0) {
    84. room[xPlayer][yPlayer] = '.';
    85. xPlayer--;
    86. room[xPlayer][yPlayer] = 'P';
    87. } else {
    88. System.out.println("You can not go there!");
    89. }
    90. break;
    91. case "d":
    92. case "right":
    93. if (xPlayer < room.length - 1) {
    94. room[xPlayer][yPlayer] = '.';
    95. xPlayer++;
    96. room[xPlayer][yPlayer] = 'P';
    97. } else {
    98. System.out.println("You can not go there!");
    99. }
    100. break;
    101. case "exit":
    102. run = false;
    103. break;
    104. // if the user input is not one of our commands print help
    105. default:
    106. System.out.println(
    107. "Command unknown! Please type up, down, left or right to move or exit to quit this program");
    108. }
    109. } while (run);
    110. System.out.println("Goodbye");
    111. }
    112. }
    Alles anzeigen
    Jedoch komme ich nicht weiter, über Lösungsansätze würde ich mich freuen!