Respuesta :
Answer:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CharTwoDArray {
public static void main(String[] args) {
String file = "word.txt";
final int MAX_CHARS = 1000;
int count = 0;
char array[] = new char[MAX_CHARS];
char array2D[][] = new char[6][7];
try {
int t;
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((t = br.read()) != -1) {
if ((char) t == '\n') continue;
array[count++] = (char) t;
}
} catch (IOException e) {
System.out.println("Error in opening file!");
System.exit(0);
}
int t = 0;
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 7; y++) {
// if no char then adding *
if (t >= count) {
array2D[x][y] = '*';
} else
array2D[x][y] = array[t++];
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(array2D[i][j]);
}
System.out.println();
}
}
}
Explanation:
- Use buffer reader for reading file .
- Add char to 1D array.
- Add 1D array to 2D array.
- Display the 2D array .