views:

356

answers:

4

hi,

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 integers 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 any buddy help me with this please ?

thanks a lot

A: 

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.

Mike Clark
thanks for your interested fine, but 6) needs adjustment, that is I need the whole file in one arrayso could you give me how to do ??
sami
A: 

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.

Stephan202
+4  A: 
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
MatlabDoug
greaaaaaat !!!!!!this single command solve my problem !!!many many thanks dude !!!!!!!!
sami
Sami, Please use the "accept answer" feature if this is the answer you liked best.
MatlabDoug
A: 

this command solve the problem

a = dlmread('a.txt')

especial thanks to "MatlabDoug",

thanks to all of who interested with my question.

thanks to "Mike C" & "Stephan202" for their efforts !!!

thanks to this great wbsite !!!

sami
@sami: What you have said here should be added as comments to other peoples answers or added to the question, not as an answer. You should delete this answer by clicking the 'delete' link just above this comment area. Also, you can chose an "accepted" answer (i.e. the one you found most helpful in answering your question) by clicking the check mark underneath the vote counts of that answer.
gnovice
@sami, use the "Accept Answer" mechanism.
Nzbuu