I'm really struggling with a piece of work, I need to load data from a text file into an array, I have managed to load the data into java and print it, but now the data needs to be split and passed to another class. I tried using .split(" ") but the data doesn't share a common amount of white space so I end up with a huge array half of which is filled with entries that have 2 lots of data or just a white space. Here is the code I have so far:
import java.io.*;
public class Reader {
int i= 0;
int k=0;
static String test;
public static void main(String[] Args){
String file ="1RainfallSample.txt";
//reading
try{
InputStream ips=new FileInputStream(file);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null){
test+=line+"\n";
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
// split(String Delimiter)
String[] dataArray=test.split("\\s");
System.out.println("Array :"+dataArray.length);
for(int i=0;i<dataArray.length;i++)
{
System.out.println("array"+i+" :"+dataArray[i]);
}
}
}
And below is a small example of the data because I don't think I explained it very well.
4 6.10 1.80 1.00 26.10 9.60 0.00 0.00 0.00
0.00 0.00 3.10 0.30 0.20 0.40 0.00 0.00 0.00 0.00 0.00 0.00
0.00 2.50 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 -99.992011
I feel like I'm approaching it all the wrong way, I'm not a good programmer as you can tell, just a nudge in the right direction would be appreciated.