so i have this java code:
String line;
int numrow=5, numcol=5;
BufferedReader reader;
String arr[][]=new String [numrow][numcol];
try{
reader=new BufferedReader (new FileReader(pname));
int row=0;
int col=0;
while((line=reader.readLine())!=null){
if(line.startsWith("Entry")){
col=0;
line=reader.readLine();
while(line!=null&&!line.startsWith("Entry")&&col<numcol){
arr[row][col]=line;
col++;
line=reader.readLine();
}
row++;
}
}
} catch (Exception e){
System.out.println("Error: "+e.getMessage());
}
}
which is meant to take entries from a file with the format:
Entry 1:
a
a
a
a
a
Entry 2:
b
b
b
b
b
Entry 3:
c
c
c
c
c
Entry 4:
d
d
d
d
d
Entry 5:
e
e
e
e
e
and send them to a 2d array with the format
a a a a a
b b b b b
c c c c c
d d d d d
e e e e e
I tried the above code but it prints out like:
a a a a a
c c c c c
e e e e e
null null null null null
null null null null null
what should i do to fix it?
(my print array code is below but it doesnt have any problem printing out another array)
for (int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
New contributor
Twiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.