Re: reading data from a file
Jonie <kimfinale@gmail.com> wrote in news:1177594640.389203.91260
@s33g2000prh.googlegroups.com:
Hello, I was wondering if anybody can help me figure out what's going
on the code below.
I wrote a method which read the data from a file and turn the data
into an array.
This method fills the last row with only zeros for some reason. I
checked and there are rows with only zeros in the original data file.
BufferedReader returns null when the end-of-file has been reached. How does
the Scanner c'tor handle being passed null?
If you've got an empty line at the end of the file, then you may be
constructing a Scanner with an empty string. How does Scanner react to this?
It also looks to me as if your array counter variable 'r' is being
incremented even if BufferedReader is returning null (indicating end of
file). You should probably put a check for nullity into this code and break
out of the loop when null is encountered.
----------------------- code snippet
----------------------------------------------
public static int[][] getDataFromFile( String filename ) throws
IOException{
int [][] graphData = new int[100][100];
preferrable to use a java.util.List instead of an array, because a
java.util.List is not fixed-size, can grow to accommodate more than 100
records of data from the file. Integer arrays are initialized to zero, so
this may be the root cause of your problem.
br = new BufferedReader( new FileReader( filename ) ) ;
int r = 0;
the variable 'r' duplicates the functionality of the variable 'i', so you
should consider removing one or combining the two into one. It is also
generally better to use longer variable names -- searching through the code
for all references to variable 'r' is painful.
for( int i = 0; i < row; i++ ){
the variable 'row' isn't defined in this context.
s = new Scanner( br.readLine() );
Why are you using the Scanner c'tor that takes a String input parameter
instead of the Scanner c'tor that takes a Readable input parameter?
BufferedReader is a Readable, so you can pass the BufferedReader to the
Scanner directly instead of having to deal with reading the input.
r += 1;
r++;
is clearer.
int col = 0;
if( r > numHeaderLines ) {
while( s.hasNext() ){
graphData[ r - 2 ][ col ] = s.nextInt();
col += 1;
}
}
}
if( s != null ) s.close();
This is Very Bad layout -- if you're stepping through this code in the
debugger, you can't tell if the branch was taken or not. Use
if (s!= null)
s.close();
instead.
if( br != null ) br.close();
return graphData;
}
public void printArray(){
for( int i = 0; i < data.length; i++ ){
System.out.println( data[i][1]+ " " + data[i][2]+ " " + data[i]
[3]+ " " + data[i][4] );
}
}
------------------------------------ output
-----------------------------------------------------
42889 1 44689 1
44160 0 39411 0
39796 1 43676 1
20430 1 44346 1
........
44300 1 44088 1
44670 1 45439 1
43291 0 41400 1
41829 0 43119 0
0 0 0 0 <----!!!!
--
---------------------------------------------------------------------
Greg R. Broderick gregb+usenet200612@blackholio.dyndns.org
A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------