Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Diem Tran   on Oct 22 In Java Category.

  
Question Answered By: Adalric Fischer   on Oct 22

The following program read  the data  shown below it as you describe.


/* DataReader.java April 19, 2003 */

import java.io.*;
import java.util.*;

/** Reads data from a text  file into a 2 dimensional
* floating point array.
* This assumes the data is formatted as row  and columns
* with each line of text representing one row of data
* and each column  is separated by a space or tab.
* If the number  of rows and columns is not known ahead
* of time, then the program has to be modified to count
* to count them and initialize the array  to the correct
* dimension.
* @author: Charles Bell
* @ version: April 19, 2003
*/
public class DataReader{

private boolean debug = false;
private float[][]data;
private String dataFileName = "data.txt";

/**
* Change the following to the number of rows
* and columns in the data file.
*/
private int  rows = 15;
private int columns = 12;

/** Constucts a DataReader object. */
public DataReader(){
data = readData();
}

/** Runs the DataReader application. */
public static void  main(String[] args){
new DataReader();

}

/** Loads the data form the text file  into a data array
* with the preconfigured dimensions.
*/
public float[][] readData(){
float[][] fileData = new float[rows][columns];
try{
FileReader fr = new FileReader(dataFileName);
BufferedReader br = new BufferedReader(fr);
String nextLine = "";
for (int row = 0; row < rows; row++){
while ((nextLine = br.readLine()) != null){
if (debug) System.out.println(nextLine);
StringTokenizer tokenizer = new
StringTokenizer(nextLine, " ");
for (int column = 0; column < columns; column++){
String nextNumberString = tokenizer.nextToken();
if (debug) System.out.println(nextNumberString);
/* By calling a method which generates an exception
* message external to this loop, the whole loop
* will not crash if a bad data entry is found.
*/
fileData[row][column] = toFloat(nextNumberString);
}
}
}
}catch(FileNotFoundException fnfe){
System.err.println("Could not find file: " + dataFileName);
}catch(IOException ioe){
System.err.println("Problem occurred while attempting"
+ " to read data from file: " + dataFileName);
}
return fileData;
}

/** Converts a data entry into a floating point value and
* signaling errors as a -1 and an error message.
*/
private float  toFloat(String s){
float f = -1;
try{
f = Float.parseFloat(s);
}catch(NumberFormatException nfe){
System.err.println("Problem occurred while converting "
+ s + " to a float value.");
}
return f;
}

}


**************************
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015
123.0 1245.5 35.3 12.4 122.2 155.7 166.8 178.8 899.7
9002.3 0.0020 0.0015

Share: 

 
 
Didn't find what you were looking for? Find more on Java programming Or get search suggestion and latest updates.


Tagged: