user18258423
user18258423

Reputation:

How to read each line of a textfile into a 2D array in Java

I am trying to create a login system in Java and I have saved admin information in a text file in this format:

Hannah,Joshua,Female,373ac,admin123

Leena,Kevin,Female,3283c,admin123

The fourth index (373ac) is their username and fifth (admin123) is the password. Each admin will get a new username and a separate line in the file.

Each time an admin logs in, they will input their name and the program should search through the file with the line that starts with their name and compare the username and password, so they can log in. I think the best way is a 2D array. I have this code so far but it's not reading it as a 2D array but just one array with all the lines. Like this:

[Hannah,Joshua,Female,373ac,admin123,Leena,Kevin,Female,3283c,admin123]

Can you please help me out?

public class ReadFile {
    public static void main(String[] args) throws Exception {
        BufferedReader bufReader = new BufferedReader(new FileReader("Admin.txt"));
        ArrayList<String> listofLines = new ArrayList<>();

        String line = bufReader.readLine();
        while (null != (line = in.readLine())) {
            listofLines.add(line);
        }

        bufReader.close();

        int [][] map = new int[bufReader.size()][];
        int q = 0;
        for (int i = 0; i < map.length; q++) {
            String[] rooms = bufReader.get(i).split(",");
            map[i] = new int[rooms.length];
            
            for (int w = 0; w < rooms.length; w++) {
                map[q][w] = Integer.parseInt(rooms[w]);
            }

            System.out.println(Arrays.toString(map[q]));
        }
    }
}

Upvotes: 0

Views: 61

Answers (2)

user17233545
user17233545

Reputation:

You can use Files.lines() to read the file as a Stream<String>.

static String[][] readFileAs2DArray(String fileName) throws IOException {
    try (Stream<String> stream = Files.lines(Path.of(fileName), Charset.defaultCharset())) {
        return stream
            .map(line -> line.split(","))
            .toArray(String[][]::new);
    }
}

public static void main(String[] args) throws IOException {
    String[][] result = readFileAs2DArray("Admin.txt");

    for (String[] line : result)
        System.out.println(Arrays.toString(line));
}

output:

[Hannah, Joshua, Female, 373ac, admin123]
[Leena, Kevin, Female, 3283c, admin123]

Upvotes: 1

mmartinez04
mmartinez04

Reputation: 323

You're populating listofLines but never using it

Instead of iterating based on bufReader, why not utilize listofLines?

Also, why are you using both i and q as indexes? One seems sufficient

Edit: The int[][] array should also be String[][]

String [][] map = new String[listofLines.size()][];

for (int i = 0;i<map.length;i++){

    String[] rooms = listofLines.get(i).split(",");
    map[i] = new int[rooms.length];
        
    for(int w = 0; w<rooms.length;w++){
        map[i][w] = rooms[w];
    }

    System.out.println(Arrays.toString(map[i]));
}

Also, if you want to make your code more succinct

String [][] map = new String[listofLines.size()][];

for (int i = 0;i<map.length;i++){

    map[i] = listofLines.get(i).split(",");

    System.out.println(Arrays.toString(map[i]));
}

For your bufReader try changing the code to this

String line = bufReader.readLine();
while (line != null) {
    listofLines.add(line);
    line = bufReader.readLine();
}

Upvotes: 1

Related Questions