werck
werck

Reputation: 75

Java - Reading from file and split

I need a little help. I want to read a text document that looks like this:

4 3 2   
3 6 9   

This is not the main part, because I could do this. The problem is with the splitting. How I can get those numbers in fully separated Arraylists? I mean, if I have three Arraylists, I want to save the first number of the line to the first ArrayList, the second number to the second Arraylist, and the third number to the third Arraylist.

Can somebody help me with that?

Upvotes: 0

Views: 1589

Answers (3)

Mike
Mike

Reputation: 11

If we consider file gona have only two lines and three numbers try doing this. I can't test this atm, but should work.

ArrayList<Integer> FirstList = new ArrayList();
Arraylist<Integer> SecondList = new ArrayList();
ArrayList<Integer> ThirdList = new ArrayList();
//add add file to read bufer
Bufferedeader buff = new Bufferedreader(FileBuffer(filePath));
//since we know how many numbers we have no need for lopps
FirstList.add(buff.read());
buff.read();
SecondList.add(buff.read);
buff.read();
//and so on

This is definatly not the best solution to the problem but might work out or even help you.

Upvotes: 1

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

Here's a little guide for you:

You have to find a way to read the file line by line. When you do, you can split(String regex) them and convert the resulting array to a List.

//Homework: 
//Add source code that reads the file line by line.
//For each of the read lines, do:
String[] split = line.split(" ");
List<String> myList = Arrays.asList(split);

Upvotes: 1

BlackBox
BlackBox

Reputation: 2233

First you look here String.split() and then you move onto Arrays.asList.

Upvotes: 2

Related Questions