1

I want to read in a simple text file and save it as an ArrayList. The file looks like so

0.2253 
0.808 
0.132 
0.341 

4.18546
8.65418
1.45535
0.341

and so on...

They will always be four lines and they will always be delimited by a blank space. Somehow I'd like to use that as the break point to begin the new array and pick back up with the first new number as index zero.

The numbers must be stored as strings.

I want to assign this data to an ArrayList such that

[0][0] = 0.2253
[0][1] = 0.808
[0][2] = 0.132
[0][3] = 0.341

[1][0] = 4.18546
[1][1] = 8.65418
[1][2] = 1.45535
[1][3] = 0.341

how can I do this using the structures of java i/o?

java io arraylist

So far I have this..

    //array list data struc
    ArrayList<String> array_list = new ArrayList<String>();

    String component_doc = "/home/joao/document.txt"

    Scanner inFile = null;
    try 
    {
        inFile = new Scanner(new File(component_doc));
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

    while(inFile.hasNextLine())
    {
        array_list.add(inFile.nextLine());
    }
  • So, what have you tried to do so far? – mkrakhin Feb 13 '15 at 12:10
  • Take a look to [Two Dimensional ArrayList](http://stackoverflow.com/questions/15285179/two-dimensional-arraylist) and [2 dimensional array list](http://stackoverflow.com/questions/10866205/2-dimensional-array-list). By the way, there is no `list` variable. You declared as`array_list`. – Albert Feb 13 '15 at 12:18
  • @Albert I know what a 2-d array is, what i don't know if how to populate one by reading in from a file as I described above. –  Feb 13 '15 at 12:21
  • 2
    Sorry, but doesn't seem you know. The way you declared `array_list` is wrong to store a 2-d `ArrayList`. It should be `ArrayList> array_list`. – Albert Feb 13 '15 at 12:25

2 Answers2

2

This is a way to read in those lines and put them in an array list.

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

protected static ArrayList<String> yourArrayList = new ArrayList<String>();
String fileName = "C:\\Users\\myComputer\\Desktop\\test_file.txt";
    try
    {
        List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
        for (int i = 0; i < lines.size(); i++)
        {
            yourArrayList.add(lines.get(i).toString());
        }
    }catch(IOException io)
    {
        io.printStackTrace();
    }
Swagin9
  • 1,002
  • 13
  • 24
0

Just a hint:

ArrayList<ArrayList<String>> array_list = new ArrayList<ArrayList<String>>(); //Create 2D arrayList

// open file and do all previous stuff 

ArrayList<String> item = new ArrayList<String>(); // Create 1D ArrayList 

while(inFile.hasNextLine())
     String str = nextLine(); 
     if (str.trim().length() == 0) {  // is blank line?
         if (item.size() > 0) array_list.add(item); // Store 1D arrayList into 2D 
         item = new ArrayList<String>();  // Create new 1D arrayList
     }
     else { 
          item.add(strLine); // Add each element to 1D arrayList
     } 
} 

Remark: This is an instance of an ArrayList, so you can't access them using array_list[X][Y]. You could try the method .toArray() to convert it to a true 2-D array.

Albert
  • 1,156
  • 1
  • 15
  • 27