简体   繁体   中英

Java, add Char's of TXT-File into a multidimensional Array with Scanner

I really need your help. I want to put the characters of a TXT-file into a Multidimensional Array, but my Java code doesn't really work. I have to read the file with the Scanner.util. Here is my Code

import java.io.*;
import java.util.Scanner;


public class Test {
public static void main(String args[]) {
try {



        FileInputStream file = new FileInputStream("a.txt");
        Scanner s = new Scanner(file);
        char arr[][];

        while (s.hasNextLine()) {
             String line = s.nextLine();
            for (int i=1 ;i<=line.length(); i++){
                for (int k=1 ;k<=line.length(); k++){
                arr[k][i] = line.charAt(i);          //the local variable arr may not have been initialized

                }
            }
            for (int i=0 ;i<=line.length(); i++){
                int k=0;
                System.out.println (arr[i][k]);      //the local variable arr may not have been initialized
                k ++;
            }
        }


        file.close();
    } catch (IOException e) {
        System.out.println("Fehler");
    }
}
}

Can you please help me to correct my Code? It would be really nice :)


EDIT:

Okay. This is what it looks like right now:

public class Test {
public static void main(String args[]) {
try {



    FileInputStream file = new FileInputStream("a.txt");
    Scanner s = new Scanner(file);
    char arr[][] = new char[15][9]; //changed

    while (s.hasNextLine()) {
         String line = s.nextLine();
        for (int i=0 ;i<=line.length(); i++){ //changed
            for (int k=0 ;k<=line.length(); k++){ //changed
            arr[k][i] = line.charAt(i);

            }
        }
        for (int i=0 ;i<=line.length(); i++){
            int k=0;
            System.out.println (arr[i][k]);
            k ++;
        }
    }


    file.close();
} catch (IOException e) {
    System.out.println("Fehler");
}
}
}

This is the TXT-File i wanted to read in:

---------------
|S    |  |    |
| --- | ----  |
|  |  |     | |
|  |  |---- |Z|
|   | |     | |
| |   |  |  | |
| | |    |    |
---------------

but if i start the program, i'll get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at maze.Test.main(Test.java:21)

do you have any idea, what i'm doing wrong? -.-


Okay. This is what it looks like right now:

public class Test {
public static void main(String args[]) {
try {



    FileInputStream file = new FileInputStream("a.txt");
    Scanner s = new Scanner(file);
    char arr[][] = new char[15][9]; //changed

    while (s.hasNextLine()) {
         String line = s.nextLine();
        for (int i=0 ;i<=line.length(); i++){ //changed
            for (int k=0 ;k<=line.length(); k++){ //changed
            arr[k][i] = line.charAt(i);

            }
        }
        for (int i=0 ;i<=line.length(); i++){
            int k=0;
            System.out.println (arr[i][k]);
            k ++;
        }
    }


    file.close();
} catch (IOException e) {
    System.out.println("Fehler");
}
}
}

This is the TXT-File i wanted to read in:

---------------
|S    |  |    |
| --- | ----  |
|  |  |     | |
|  |  |---- |Z|
|   | |     | |
| |   |  |  | |
| | |    |    |
---------------

but if i start the program, i'll get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at maze.Test.main(Test.java:21)

do you have any idea, what i'm doing wrong? -.-

you need to initialize the array eg,

    char arr[][] = new char[100][100];

also in the first loop, the variables i and k should start with 0. Plus remove <= and change to less than

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM