0

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?

2
  • 11
    We can, but we first want to see what you have tried. Commented Oct 4, 2013 at 14:28
  • 1
    I foresee a Scanner in your future Commented Oct 4, 2013 at 14:30

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.