Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Java programming

  Asked By: Diem    Date: Oct 22    Category: Java    Views: 948
  

I'm just starting out in Java programming, and I was hoping that
someone could possibly suggest to me how I could modify the following
code to read the data in from the text file and store it into a 2
dimensional array instead of just a 1 dimensional array.


dataFile = new BufferedReader(new FileReader("c:\\data.txt"));

public float[] getOneLine() throws IOException,NumberFormatException
{
String dataLine = dataFile.readLine();
StringTokenizer tokenizer = new StringTokenizer(dataLine);
int numTokens = tokenizer.countTokens();
float[] temps = new float[numTokens];
for (int i = 0; i < numTokens;i++){
String number = tokenizer.nextToken();
data[i] = Float.parseFloat(number);
}
return data;
}

private void storeData() throws IOException
{
int index = 0;
for (int row = 0; row < 12; row++){
float[] thisRow = reader.getOneLine();
for (int column = index; column < thiscolumn.length;
column++){
array[row][column] = thisRow[column];
}
row++;
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Adalric Fischer     Answered 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

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




Tagged: