开发者

how to read digits from text file and save it in an array "using MATLAB or JAVA"?

开发者 https://www.devze.com 2022-12-08 21:16 出处:网络
I have similar problem with little changes, which is: I have text file contain large number of lines with different sizes \"i.e. not all lines has same length\"

I have similar problem with little changes, which is:

I have text file contain large number of lines with different sizes "i.e. not all lines has same length" each line contain in开发者_StackOverflowtegers only.

as an example A.txt =

4 6 4 1 2 2 5 7 7 

0 9 5 5 3 2 43 3 32 9 0 1 3 1

3 4 5 6 7 4  

34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8 4 2 1 5 

I want to put these integers into an array so each integer will be an element in the array and saving lines from "overlapping" i.e. I need to keep each line as it is.

Could anybodyy help me with this?


a = dlmread('a.txt')

a =

Columns 1 through 21

 4     6     4     1     2     2     5     7     7     0     0     0     0     0     0     0     0     0     0     0     0
 0     9     5     5     3     2    43     3    32     9     0     1     3     1     0     0     0     0     0     0     0
 3     4     5     6     7     4     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
34     5     8     9     0     7     6     2     4     5     6     6     7     5     4     3     2    21     4     9     8

Columns 22 through 25

 0     0     0     0
 0     0     0     0
 0     0     0     0
 4     2     1     5


I would do the following:

1) Create a new Array for each line

2) Read lines, one at a time from file

3) Split each line by the "space" char

4) Iterate through the String[] that you get from the split operation, passing each value to Integer.parseInt(value);

5) Store value in array;

6) When reading next line, create new array to store the new lines values.


You can read the data using Scanner, one line at a time, and store the numbers in a List, e.g. an ArrayList:

import java.util.*;
import java.io.*;

public class Numbers
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Scanner data = new Scanner(new File("A.txt"));
    List<List<Integer>> ints = new ArrayList<List<Integer>>();

    while (data.hasNextLine()) {
      List<Integer> lineInts = new ArrayList<Integer>();
      Scanner lineData = new Scanner(data.nextLine());

      while (lineData.hasNextInt()) {
        lineInts.add(lineData.nextInt());
      }

      ints.add(lineInts);
    }

    System.out.println(ints);
  }
}

This code opens the file for reading, and creates a two-dimensional ArrayList. The outer list contains a list for each line in the file. The inner lists contain the integers on the respective lines. Note that empty lines result in empty lists. Also, you will have to properly handle any IO exception, unlike the code shown above.

If you really want the integers in a two dimensional array instead of an ArrayList, then you'll either have to call toArray, or alter the code above. That is left as an exercise for the reader.

0

精彩评论

暂无评论...
验证码 换一张
取 消