0

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.

1
  • first change test+=line+" " cause u want space for delimeter .and u can use regex inside split . for spaces .split("\s+") it will use spaces as split Commented May 11, 2013 at 16:32

2 Answers 2

2

You can use

String[] dataArray = test.split("\\s+");

to get rid of extra white spaces.

Sign up to request clarification or add additional context in comments.

5 Comments

Holy crap you guys answer questions fast!
:) Programmers have way too much free time.
Haha well ty to both of you, that worked a treat I now have an array of 760 ish entries instead of 3000 ish, now to work out how to pass this data to the days class :p. Thanks a lot.
One follow up question i'd ask is how do I then convert the string array into a float, "public float[] (String dataArray[i]);" I tried putting this in the for loop, but to no avail :\
@EarlLemongrab: You can declare a float[] floatArray = new float[dataArray.length] and use floatArray[i] = Float.valueOf(dataArray[i]).
1

Alternatively if you want to tidy your data, you could match duplicate spaces and remove them or replace with a single space.

First make sure your text is in a string variable, then:

Val = Val.replaceAll(" {2,}", " ");

1 Comment

Thanks @ridoy. I couldn't figure out how to mark code blocks on the mobile app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.