Reputation: 195
I am trying to read a file and place the contents into a 2D array starting from line 5. I do get the program to run without errors, but something goes wrong and it does not fill the array.
I tested around and found that something seems to go wrong after I introduce a while loop. The first 4 lines of the file are read successfully one by one outside the while loop, so the problem should not be with the filereader itself.
This is the while loop that should read the remaining lines and place each character of each string into a 2D array:
while (fileReader.hasNextLine()) {
String fileLine = fileReader.nextLine();
int k = 0;
for (int i = 0; i < fileLine.length(); i++) {
for (int j = 0; j < columns; j++) {
fileArray[k][j] = fileLine.charAt(i);
}
}
k++
}
However, if I try to print the array, it simply prints empty lines. Why doesn't it fill the array and why doesn't it give any errors? And of course, how could I fix this code to make it work?
// Edit. Here is the entire code, as something more seems to be wrong with it than just the for loop (which I fixed for this):
import java.io.*;
import java.util.Scanner;
public class Filer {
public static void main (String[] args) {
Scanner fileReader = null;
char[][] fileArray = null;
try {
String fileName = "alphabet.txt";
File textFile = new File(fileName);
fileReader = new Scanner(fileName);
String line1 = fileReader.nextLine();
int rows = Integer.parseInt(line1);
String line2 = fileReader.nextLine();
int columns = Integer.parseInt(line2);
fileArray = new char[rows][columns];
char line3 = fileReader.nextLine().charAt(0);
char line4 = fileReader.nextLine().charAt(0);
int i = 0;
while (fileReader.hasNextLine()) {
String fileLine = fileReader.nextLine();
for (int r = 0; r < fileLine.length(); r++) {
fileArray[i][r] = fileLine.charAt(r);
}
i++;
}
fileReader.close();
}
catch (Exception e) {
if (fileReader != null) {
fileReader.close();
}
}
if (fileArray != null) {
for (int i = 0; i < fileArray.length; i++) {
System.out.print(fileArray[i]);
System.out.println();
}
}
else {
System.out.println("Error!");
}
}
}
The file contents are:
4
4
x
o
xxxx
xxox
ooxo
xxxx
Upvotes: 1
Views: 115
Reputation: 78955
Earlier, you had some syntax error but I can see that you have corrected them now. Given below is the working code. Run it and let me know if it works.
import java.io.File;
import java.util.Scanner;
public class Filer {
public static void main(String[] args) {
Scanner fileReader = null;
char[][] fileArray = null;
try {
File file = new File("alphabet.txt");
if (file.exists()) {
fileReader = new Scanner(file);
} else {
System.out.println("The file does not exist");
}
String line1 = fileReader.nextLine();
int rows = Integer.parseInt(line1);
String line2 = fileReader.nextLine();
int columns = Integer.parseInt(line2);
fileArray = new char[rows][columns];
char line3 = fileReader.nextLine().charAt(0);
char line4 = fileReader.nextLine().charAt(0);
int i = 0;
while (fileReader.hasNextLine()) {
String fileLine = fileReader.nextLine();
for (int r = 0; r < fileLine.length(); r++) {
fileArray[i][r] = fileLine.charAt(r);
}
i++;
}
fileReader.close();
} catch (Exception e) {
if (fileReader != null) {
fileReader.close();
}
}
if (fileArray != null) {
for (int i = 0; i < fileArray.length; i++) {
System.out.print(fileArray[i]);
System.out.println();
}
} else {
System.out.println("Error!");
}
}
}
Upvotes: 1
Reputation: 105
How about like this?
int i = 0;
while (fileReader.hasNextLine()) {
String fileLine = fileReader.nextLine();
for (int r = 0; r < fileLine.length(); r++) {
fileArray[i][r] = fileLine.charAt(r);
}
i++;
}
You only need to loop through the line in the file (The for loop I have here). No other loop is necessary.
Upvotes: 1