0

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.

3
  • Seems like a duplicate of stackoverflow.com/questions/19844649/…. In general, for converting an Array into a List, you can use Arrays.asList(myArray); Commented Apr 5, 2015 at 23:53
  • I keep getting the error "cannot convert List<String> to List<Person>... The ArrayList holds Person objects FYI Commented Apr 6, 2015 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. Commented Apr 6, 2015 at 0:15

2 Answers 2

0
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/

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

Comments

0

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

    }
}

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.