The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RE: Arrays

Posted by Kishori Sharan on August 03, 2000 at 3:59 PM

Hi
Here is what I understand from you question. You have a text file which consists of 7 columns and many rows. The column contains numeric values and you want to read them in java program so that you can use them. I have writtem a program with the following assumptions:
1. You have a text file with 7 columns.
2. Each row is separated by new line.
3. Each column value is separated by tab.
The program reads all the values in a two dimensional double array ( here it also prints the value on console. ) . I am also giving you the file contents ( for points.txt ).

Thanx
Kishori

////////////// Program Points.java ////////////
import java.util.* ;
import java.io.* ;
import java.text.* ;
public class Points {

private double[] parseStr ( String str ) {
double[] nums = new double[7] ;
DecimalFormat df = new DecimalFormat ( "0.00" ) ;
Number n = null;
String temp;
int counter = 0 ;
int start = 0;
int end = 0 ;

if ( str == null )
return nums ;

// Find the first position of tab
end = str.indexOf ( '\t', start ) ;
while ( end > 0 ) {

// Get the string between the start and end position
temp = str.substring ( start, end ) ;
try {
n = df.parse ( temp ) ;
if ( counter < nums.length )
nums[counter++] = n.doubleValue ( );

} catch ( Exception e ) {
}

// Read the next number
start = end + 1 ;
end = str.indexOf ( '\t', start ) ;
}

// Read the last number
start = str.lastIndexOf ( "\t" ) + 1 ;
end = str.length() ;

// Get the string between the start and end position
temp = str.substring ( start, end ) ;
try {
n = df.parse ( temp ) ;
if ( counter < nums.length )
nums[counter++] = n.doubleValue ( );

} catch ( Exception e ) {
}

return nums ;
}

public static void main ( String[] args ) throws Exception {
int rows = 0 ;
int rowCounter = 0 ;
Points nn = new Points () ;
BufferedReader br = new BufferedReader ( new InputStreamReader
( new FileInputStream ( "C:\\kishori\\points.txt" ) ) );

// Read total number of lines in file
String str = br.readLine ( );
while ( str != null ) {
rows++ ;
str = br.readLine ( );
}
rows-- ; // Decrement by one because rows is incremented by one after reading the line
double[][] points = new double[rows][7] ;

br.close () ;
br = new BufferedReader ( new InputStreamReader ( new FileInputStream ( "C:\\kishori\\points.txt" ) ) );


do {
// Read the first line
str = br.readLine ( );

// Parse the first line read
if ( rowCounter < rows && str != null )
points[rowCounter++] = nn.parseStr ( str ) ;

} while ( str != null ) ;

for ( int i = 0 ; i < points.length ; i ++ ) {
for ( int j = 0; j < points[i].length ; j++ )
System.out.print ( points[i][j] + "\t" );

System.out.println ();
}

// Close the stream
br.close ( );
}

}

//////////////////////// points.txt ////////////////////
100.11 100.12 100.13 100.14 100.15 100.16 100.17
200.11 200.12 200.13 200.14 200.15 200.16 200.17
300.11 300.12 300.13 300.14 300.15 300.16 300.17
400.11 400.12 400.13 400.14 400.15 400.16 400.17
500.11 500.12 500.13 500.14 500.15 500.16 500.17






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us