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.
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();
}
}
CodePudding user response:
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!
CodePudding user response:
You don't need a nested loop to do this.
- first, forget about the columns, all you need is the rows to allocate the array to hold all the lines read in.
- read in the dimensions and capture the rows using split.
- allocate the character array for that many rows.
- Now read in the file line by line, replacing spaces with empty strings and then call
toCharArray()
and use that array to populate the columns of your 2D array.
File file = new File("f:/griddata.txt");
try (Scanner scan = new Scanner(file)) {
String[] dimensions = scan.nextLine().split("\\s*,\\s*");
int rows = Integer.parseInt(dimensions[1]);
char[][] tabLettres = new char[rows][];
int row = 0;
while (scan.hasNextLine()) {
tabLettres[row ] = scan.nextLine().replace(" ", "")
.toCharArray();
}
// print results
for (char[] char_row : tabLettres ) {
System.out.println(Arrays.toString(char_row));
}
} catch (FileNotFoundException fne) {
fne.printStackTrace();
}
prints
[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]