4

I really new to Java so I'm having some trouble figuring this out. So basically I have a text file that looks like this:

1:John:false  
2:Bob:false    
3:Audrey:false

How can I create an ArrayList from the text file for each line?

4
  • 2
    Do you even try anything before posting a query on stackoverflow? Commented Apr 6, 2016 at 3:21
  • Thankyou all so much!!!! Commented Apr 6, 2016 at 3:24
  • yea i do i have all my code if you wanna see it Commented Apr 6, 2016 at 3:25
  • 1
    I was asking to encourage you to look up tutorials, books and documentation before posting on stackoverflow. That is a better way to learn. Commented Apr 6, 2016 at 3:29

5 Answers 5

11

Read from a file and add each line into arrayList. See the code for example.

public static void main(String[] args) {

        ArrayList<String> arr = new ArrayList<String>();
        try (BufferedReader br = new BufferedReader(new FileReader(<your_file_path>)))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                arr.add(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 

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

2 Comments

Thankyou so much! this helped I had to specify what type of array i needed other than that perfect thankyou!
Ohh Yes. I missed it . Sorry. Can you please accept this answer? <Click on the tick to accept>
1

While the answer above me works, here's another way to do it. Make sure to import java.util.Scanner.

public static void main(String[] args) {
     ArrayList<String> list = new ArrayList<String>();
     Scanner scan = new Scanner("YOURFILE.txt");
     while(scan.hasNextLine()){
         list.add(scan.nextLine());
     }
     scan.close();
}

Comments

1

If you know how to read a file line by line, either by using Scanner or by using BufferedReader then reading a text file into ArrayList is not difficult for you. All you need to do is read each line and store that into ArrayList, as shown in following example:

BufferedReader bufReader = new BufferedReader(new 
FileReader("file.txt"));
ArrayList<String> listOfLines = new ArrayList<>);
String line = bufReader.readLine(); while (line != null)
{
    listOfLines.add(line);
    line = bufReader.readLine();
} 
bufReader.close();

Just remember to close the BufferedReader once you are done to prevent resource leak, as you don't have try-with-resource statement

Comments

0

This will be help to you.

List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("list.txt"));
String line;
while ((line = reader.readLine()) != null) {
    list.add(line);
}
reader.close();

Then you can access those elements in the arraylist.

Comments

0

java 8 lets you do this

String fileName = "c://lines.txt";
        List<String> list = new ArrayList<>();

        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            list = stream
                    .map(String::toUpperCase)
                    .collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        list.forEach(System.out::println);

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.