Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Array help needed please

  Asked By: Vernon    Date: Dec 01    Category: Java    Views: 937
  

I'm trying to define a multple dimensional array of doubles to store
rainfall for each day of each month of a non leap year. And then read
the data into the array from a text file with 12 lines of data (ie 1
line for each month).
row number of columns
0 31
1 28
2 31
3 30 etc.

I'm having trouble starting to define the array
int rain[][] = new int[12][] <- but how do I define the number of columns

after that how do I read each row of text into its assigned row in the
array?

Any help will be greatly appreciated.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Katrina Edwards     Answered On: Dec 01

For multiple arrays with different lengths use:
double[][] rain = new double[12][];
rain[0] = double[31];
rain[1] = double[28];
.
.
.

To read the data  from a file  try using DataInputStream or FileReader
or maybe StreamTokenizer, depending on the data format inside of the
file.

 
Answer #2    Answered By: Eddie Austin     Answered On: Dec 01

make that:
double[][] rain = new double[12][];
rain[0] = new double[31];
rain[1] = new double[28];
.
.
.

 
Answer #3    Answered By: Antonio Dunn     Answered On: Dec 01

Why not use a collection class such as
java.util.ArrayList?

Then it becomes

List rainfallList[] = new ArrayList[12];

// Read file  and get data
...
rainfallList[0].add( ... );
etc.

 
Answer #4    Answered By: Holly Brown     Answered On: Dec 01

/* RainfallDataReader.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 a row of data for one month
* and each column or day  of data is separated by
* a space or tab. Since some months do not have
* 31 days, these array  entries will be null and
* ignored.
* @author: Charles Bell
* @ version: April 19, 2003
*/
public class RainfallDataReader{

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

/* Twelve month  in a year. */
private int rows = 12;
/* Max number  of days i a month */
private int columns = 31;

/** Constucts a RainfallDataReader object. */
public RainfallDataReader(){
rainfallData = readData();
}

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

}

/** 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, " ");
int numberOfDays = tokenizer.countTokens();
for (int column = 0; column < numberOfDays; 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;
}

}

 
Didn't find what you were looking for? Find more on Array help needed please Or get search suggestion and latest updates.




Tagged: