Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a text file that contains a sequence of characters that has the format of a 2d array and I want to load those characters into a 2d array

This is what the text file looks like:

15, 10
E E V I S S I M D N O F O R P

V S S P E V O Y A G E S U A P

N I S B E R E V E R T U E U X

O O R A C J I N F I C O N R D

I N D G O G O U P A N H I U T

S E E U U U R U D B A N A N E

A G C E T L L U R E R T M N G

C A R T E E E P E N S I O N D

C R E T R U E H C O A I D E U

O O T E V I V A C E C L R E B

The first line is the dimensions of the grid of characters.
The problem I’m having is that it is not reading the entire line of the grid.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is what I’ve come up with :

File myFile = new File(file);
    Scanner myReader = null;
    try {
        myReader = new Scanner(myFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    if (myFile.exists()) {

        String firstLine = myReader.nextLine();

        // cols
        int cols = Integer.parseInt(firstLine.charAt(0) + "" + firstLine.charAt(1));
        // rows
        int rows = Integer.parseInt(firstLine.charAt(4) + "" + firstLine.charAt(5));

        tabLettres = new char[rows][cols];
        char[] lineArray = null;
        String line = "";
        
        for (int i = 0; i < tabLettres.length; i++) {
            line = myReader.nextLine();
            lineArray = line.toCharArray();
            for (int j = 0; j < tabLettres[i].length; j++) {
                tabLettres[i][j] = lineArray[j];
            }
        }

What I’m getting is :

E E V I S S I M

V S S P E V O Y

N I S B E R E V

O O R A C J I N

I N D G O G O U

S E E U U U R U

A G C E T L L U

C A R T E E E P

C R E T R U E H

O O T E V I V A

As you can see it is not reading until the full length of the columns.
This is also the method I’m using to print the array

public void loadArray() {
    for (int i = 0; i < tabLettres.length; i++) {
        for (int j = 0; j < tabLettres[i].length; j++) {
            System.out.print(tabLettres[i][j] + " ");
        }
        System.out.println();
    }
}

>Solution :

You’re reading in the spaces here. Once you read your line, run line.replaceAll(" ", "") to get rid of these spaces. That way, you’ll only read in the relevant characters. Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading