Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

How can I read a file into a array of String[] and then convert it into ArrayList?

I can't use an ArrayList right away because my type of list is not applicable for the arguments (String).

So my prof told me to put it into an array of String, then convert it.

I am stumped and cannot figure it out for the life of me as I am still very new to java.

share|improve this question
    
Seems like a duplicate of stackoverflow.com/questions/19844649/…. In general, for converting an Array into a List, you can use Arrays.asList(myArray); –  Mick Mnemonic Apr 5 at 23:53
    
I keep getting the error "cannot convert List<String> to List<Person>... The ArrayList holds Person objects FYI –  Clarkie_12 Apr 6 at 0:12
    
It would help if you shared the relevant code. But in short, you cannot read into a list of Persons from the file without populating each Person explicitly. It might be easiest to read the "raw" string lines in first and do the conversion of line -> Person afterwards. –  Mick Mnemonic Apr 6 at 0:15

2 Answers 2

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tsenyurt on 06/04/15.
 */
public class ReadFile
{
    public static void main(String[] args) {

        List<String> strings = new ArrayList<>();
        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("/Users/tsenyurt/Development/Projects/java/test/pom.xml"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                strings.add(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

there is a code that reads a file and create a ArrayList from it

http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

share|improve this answer

Well there are many ways to do that, you can use this code if you want have a List of each word exist in your file

public static void main(String[] args) {
    BufferedReader br = null;
    StringBuffer sb = new StringBuffer();
    List<String> list = new ArrayList<>();
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(
                "Your file path"));

        while ((sCurrentLine = br.readLine()) != null) {
            sb.append(sCurrentLine);
        }
        String[] words = sb.toString().split("\\s");
        list = Arrays.asList(words);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    for (String string : list) {
        System.out.println(string);

    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.