1

i am new to java so i need help... i have a file which contains:-

Model
A
T
ENMDL
Model
A
T
ENMDL

.... repeat multiple times and i need to make a program which separate them and store them in different arraylists. can anyone help..

public ArrayList<String>  GetAllFile(String File) throws IOException
        {
            FileReader fr=new FileReader(File);
            BufferedReader br=new BufferedReader(fr);
            String rowData;
            ArrayList<String> allFile = new ArrayList<String>();

            while((rowData=br.readLine())!=null)
                if(rowData.startsWith("MODEL"))
                allFile.add(rowData);


            fr.close();

            return allFile; 

        }
        }
5
  • 1
    ArrayList<ArrayList<String>> Commented Jul 24, 2017 at 11:57
  • 1
    Your question is unclear. What is "them"? What is supposed to be in each of the array lists? In your source it seems you only have one array list. Please explain the required result and where you got stuck in your solution. Commented Jul 24, 2017 at 12:06
  • And what is your question? We won't finish your code for you. But we happy to answer specific questions here. Commented Jul 24, 2017 at 12:13
  • Your condition is not going to work because "MODEL" is not the same as "Model". Your return type should be List<List<String>>. Commented Jul 24, 2017 at 12:16
  • every model in my file has to be in a separate arraylist and i have no clue how to do it..the codes i have written only gives me an arraylist containning only models in single arraylist Commented Jul 25, 2017 at 8:05

2 Answers 2

2

Change your return type.

public static List<List<String>> fileToArrayList(String fileName) {

Create the outer container.

List<List<String>> allFile = new ArrayList<>();

Then outside of your loop.

List<String> modelLines = new ArrayList<>();

Then the condition inside of your loop should be.

if(rowData.startsWith("Model")){
    modelLines = new ArrayList<>();
    allFile.add(modelLines);
} else{
    modelLines.add(rowData);
}
Sign up to request clarification or add additional context in comments.

4 Comments

i think you need to continue when rowData equals ENMDL ?
@Yazan probably, the op doesn't appear to clear on that. The main thing I hope they get is the 2D List.
thanks it work..and just one more thing its not adding model in the arraylist
@Walizebkhan when it creates a new array list it doesn't add the rowData, it just creates the ArrayList. If you want it to add the line containing model, then add the line, modelLines.add(rowData);
0

Here is an solution that might suit you:

    public class FileToArrayList {



public static void main(String[] args) {
        // Get the file as an List.
        List<String> fileAsList = FileToArrayList.fileToArrayList("SomeFile.txt");
        // Print the lines.
        for (String oneLine : fileAsList) {
            System.out.println(oneLine);
        }
    }

    public static List<String> fileToArrayList(String fileName) {
        // Container for the lines.
        List<String> lines = new ArrayList<>();
        // Try with resources, it will close it automatically afterwards.
        try(FileReader fr = new FileReader(new File(fileName))) {
            BufferedReader br = new BufferedReader(fr);
            String line;
            // line = br.readLine() is an expression which will return line, therefore
            // we can check if that expression is not null, because
            // when its null, we reached EOF (end of file)
            while((line = br.readLine()) != null) {
                lines.add(line);
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    }

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.