1

I have 1,000 lines of data in a text file and I would like each line to be its own float [].

1,1,1,1,1,1
2,2,2,2,2,2
3,3,3,3,3,3

Would result in:

 float[0] = {1,1,1,1,1,1}
 float[1] = {2,2,2,2,2,2}
 float[2] = {3,3,3,3,3,3}

Is this possible? I could only find examples of loading an entire file into an array. I tried hardcoding all the arrays, but exceeded the byte character limit of ~65,000

2
  • 1
    Is each line the same number of floats? Commented Sep 21, 2012 at 2:43
  • Yes, each line has the same number of floats. Commented Sep 21, 2012 at 2:50

4 Answers 4

3

Try the following:

// this list will store all the created arrays
List<float[]> arrays = new ArrayList<float[]>();

// use a BufferedReader to get the handy readLine() function
BufferedReader reader = new BufferedReader(new FileReader("myfile.txt"));

// this reads in all the lines. If you only want the first thousand, just
// replace these loop conditions with a regular counter variable
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    String[] floatStrings = line.split(",");
    float[] floats = new float[floatStrings.length];
    for (int i = 0; i < floats.length; ++i) {
        floats[i] = Float.parseFloat(floatStrings[i]);
    }
    arrays.add(floats);
}

Note that I haven't added any exception handling (readLine(), for example, throws IOException).

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

Comments

0
  • use a LineIterator to read each line without loading the whole file

  • for each line, use a regular expression to extract figures like (\d\.)+ and iterator over the matches found with methods like find() and group()

Comments

0

<body>
<pre>

import java.io.FileReader;

public class Check {

    public static void main(String[] args) {    
        readingfile();
    }
    public static void readingfile() {
        try {
            FileReader read = new FileReader("D:\\JavaWkspace\\numbers.txt");
            int index;
            String nums1 = "";

            while ((index = read.read()) != -1) {
                if (((char) index) != '\n') {
                    nums1 += String.valueOf((char) index);
                }
            }

            System.out.println("Problem statement: Print out the greatest number on each line:\n" + nums1);

            String f = nums1.substring(0, 14);
            String s = nums1.substring(15, 29);
            String t = nums1.substring(30);

            String[] fs = f.split(",");
            int size = fs.length;
            int[] arr = new int[size];
            for (int i = 0; i < size; i++) {
                arr[i] = Integer.parseInt(fs[i]);
            }
            int max = arr[0];
            for (int i = 0; i < arr.length; i++) {
                if (max < arr[i]) {
                    max = arr[i];
                }
            }
            System.out.println("\nGreatest number in the first line is:" + (max));

            String[] sstr = s.split(",");
            int size2 = sstr.length;
            int[] arr2 = new int[size2];
            for (int i = 0; i < size2; i++) {
                arr2[i] = Integer.parseInt(sstr[i]);
            }

            int max2 = arr2[0];
            for (int i = 0; i < arr2.length; i++) {
                if (max2 < arr2[i]) {
                    max2 = arr2[i];
                }
            }
            System.out.println("\nGreatest number in the second line is:" + (max2));

            String[] str3 = t.split(",");
            int size3 = str3.length;
            int[] arr3 = new int[size3];
            for (int i = 0; i < size3; i++) {
                arr3[i] = Integer.parseInt(str3[i]);
            }

            int max3 = arr3[0];
            for (int i = 0; i < arr3.length; i++) {
                if (max3 < arr3[i]) {
                    max3 = arr3[i];
                }
            }
            System.out.println("\nGreatest number in the third line is:" + (max3));
            read.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
</pre>
</body>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

Loop over the line-delimited contents of the file with .split("\n") and then cast each result as float array. Here's how to convert the string into a float for you => http://www.devdaily.com/java/edu/qanda/pjqa00013.shtml

2 Comments

-1, this isn't an answer at all. You need some detail. Any detail.
That is literally what I just said.

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.