user4132350
user4132350

Reputation:

reading from file to arraylist

I have a file, which include these numbers devided with white spaces and line.

1 11 23 1 18 9 15 23 5
11 1 18 1 20 5 11 1

I want to put them into ArrayList, one line in one ArrayList (without white spaces, only numbers), and another in another ArrayList.

I tried to do it in many ways, loading numbers to Char Array, to String and then to Char Array (but decimal numbers were devided) and so on..

My piece of code:

Scanner plik1 = new Scanner( new File("plik1.txt") );

ArrayList<Integer> list = new ArrayList<Integer>();

    while (plik1.hasNext()){
        list.add(plik1.nextInt());
    }

    System.out.print("strin1 : " + list);

and what shows in output:

strin1 : [1, 11, 23, 1, 18, 9, 15, 23, 5, 11, 1, 18, 1, 20, 5, 11, 1]

However, I want it to look like:

strin1 : [1, 11, 23, 1, 18, 9, 15, 23, 5]
strin2 : [11, 1, 18, 1, 20, 5, 11, 1]

Upvotes: 1

Views: 503

Answers (5)

PDStat
PDStat

Reputation: 5825

A slight variation without using Scanners or BufferedReaders. Java 7 can do this

String plik1= new String(Files.readAllBytes(Paths.get("C:\\plik1")));
String[] lines = plik1.split("\r\n");
List<List<Integer>> intLists = new ArrayList<>();
for(int i=0; i<lines.length; i++) {
   List<String> strList = Arrays.asList(lines[i].split(" "));
   List<Integer> intList = new ArrayList<>();
   for(String s : strList) {
      intList.add(Integer.parseInt(s));
   }
   intLists.add(intList);
}

Upvotes: 0

nem035
nem035

Reputation: 35481

Problem with your approach is that you are only creating a single list.

An easy way to achieve what you want is to use a BufferedReader to read lines from a file. Then, for each line read, you create and fill up a List. You can maintain these Lists in another List for reference.

List<List<String>> myLists = new ArrayList<List<String>>();
String line = null;
BufferedReader br = null;
try {
    br = new BufferedReader(new FileReader("plik1.txt"));
    while ((line = br.readLine()) != null) {
        myLists.add(Arrays.asList(line.split(" ")));      
    } 
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}      
// I've left it out for brevity but don't forget to close resources !

// print the lists in the format you need
int counter = 1;
for(List<String> list : myLists) {
    System.out.println("strin" + counter + " : " + list);
}

Upvotes: 0

Stav Saad
Stav Saad

Reputation: 599

Here is that java 8 easy answer for that:

List<List<String>> result = Files.readAllLines(Paths.get("plik.text")).stream().map(s -> Arrays.asList(s.split(" "))).collect(Collectors.asList());

Upvotes: 2

Aleksei Shestakov
Aleksei Shestakov

Reputation: 2538

You can read file line by line and parse every line with new Scanner:

Scanner plik1 = new Scanner( new File("plik1.txt") );

ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();

// read first line
Scanner scan = new Scanner(plik1.nextLine());
while (scan.hasNext()){
    list.add(scan.nextInt());
}

// read second line
scan = new Scanner(plik1.nextLine());
while (scan.hasNext()){
    list2.add(scan.nextInt()); // second list
}

System.out.print("strin1 : " + list);
System.out.print("strin2 : " + list2);

Upvotes: 0

brso05
brso05

Reputation: 13222

You could do a list of lists something like this:

Scanner plik1 = new Scanner( new File("plik1.txt") );

ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

while (plik1.hasNextLine()){
    String temp = plik1.nextLine();
    String[] splitString = temp.split(" ");
    ArrayList<Integer> tempList = new ArrayList<Integer>();
    list.add(tempList);
    for(int i = 0; i < splitString.length; i++)
    {
       tempList.add(Integer.parseInt(splitString[i]));
    }
}

System.out.print("strin1 : " + list);

Upvotes: 1

Related Questions