Can someone help me please!

I would like to know of any advice about reading data from a file, and then storing them in arrays. I have got to work with this set of data, which needed to be preprocessed first in the following way: The first set of ten data in the file would be stored in the first row an array, and the 11th data in the same file would be stored in another the first row of another array. I will post some of these data below to give you a vivid picture of what I am trying to explain:

Each row of the first array (lets name it an input array) would have ten columns containing the first ten data in the file, i.e. 0.40627, 0.41822, 0.45128, ..., 0.90431. Then the next data in the file, i.e. 0.93279, would be stored in another array(having only one column, let us name it an output array). The next data, i.e.,0.95022 would then be stored in the second row of the output array. The second row of the input array would contain 10 data starting from 0.41822 to 0.93279. Then the next data in the file, i.e. 0.95826 would be stored in the 3rd row ow the output array, and the data from 0.95022 to 0.45128 would be stored in the third row of the input array. This sequence would continue like this until the whole data in the file are read and stored in the corresponding arrays.

I would be very grateful if any one could give me an idea on how to go about this problem. Thanks

8 Years
Discussion Span
Last Post by debee

I don't really understand why you are striping the data into those arrays quite like that (if I'm even reading your intent correctly), but I guess that's beside the point. I'd recommend keeping a 12 element array of the values you have read from the file, place the "input" values into the correct positions in your "input" array, copy over the "outputs" in the same manner, read the next item from the file, roll off the value you no longer need from the working array and add the new value, and repeat the whole process until you have reached the end of the file.

You can maintain a "head" index to the "first" data item in the working array and not bother with actually shifting the array contents in memory. To add a new item just advance the head index and overwrite the value at the previous insertion point. The copy operations should start from that head index.

Hopefully that makes sense and I haven't misunderstood how you are wanting to write out the values.

Edit: If you don't know how many elements will be in the file, you'll also have to manage the resizing of the target arrays as well, obviously.


Thanks for your idea. The reason why I am stripping the data into these arrays is that I want to work with two matrices, one, with input data, and the other with output data. These two matrices would be used in further processes. The only difficulty i am having at the moment is to read these data and strip them into these matrices.
Sorry, although I got your idea, I am still finding it difficult to code it. I am literally a beginner in Java programming. If it would be possible to code it up, and then I will be able to modify it to suit what i intend using them for.

Thanking you.


Code up what you can and if you have troubles post that code with your questions. Writing the code for you isn't the point of the forums.


I have tried coding it up, but I get this error that says: Exception in thread "main"java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine<Scanner.java.1516>.
at cat1.main<cat1.java:59>

The code is as follows:

import java.io.*;
import java.util.Scanner;
public class cat1
   public static void main(String[] args) throws IOException

	  BufferedReader out =new BufferedReader(new FileReader("Mackey..java"));
	  String z;
	  int t = 0;
	   while (out.readLine()!= null)
	  	       z = out.readLine();
	 int u = t-10;
	 // prints out the number of data in the file

     Double workingArray[] = new Double[10];
     Double input[][] = new Double[u][10];
     Double desired[][] = new Double[u][1];

     Scanner in =new Scanner(new FileReader("Mackey..java"));
     String s;
     int j, i = 0;
     // reads the first ten data into the working array
     while (in.hasNextLine() && i < 10)
       s = in.nextLine();
       Double p = Double.parseDouble(s);
       workingArray[i] = p;

    //for( i = 0; i < 10; i++)

    // copies the content of the working array into the input array
	 for(j = 0; j< workingArray.length; j++)
	 	     input[i][j]= workingArray[j] ;

   // reads the eleventh data into the array desired
    s = in.nextLine();
	Double k = Double.parseDouble(s);
	i = 0;
	desired[i][0] = k;

   while(in.hasNextLine() )

        for(i = 1; i <= u; i++)
          s = in.nextLine(); // note: this is the line 59 where the error is spotted
		  Double m = Double.parseDouble(s);
		  //if(i != u)
		  desired[i][0] = m;

          for(j = 0; j< workingArray.length; j++)
			  if(j == workingArray.length - 1)
			    {workingArray[j] = m;}
			 { workingArray[j] = workingArray[(j+1)];}

                     for(j=0; j<workingArray.length; j++)
			        input[i][j]= workingArray[j] ;

Can you figure out what i am doing wrong please?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.