0

I am reading a file and want to save it in an array list to be able to call this method in another class for later use. Here's my code:

    package com.qmul.rfid.reader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ReadFile {

        public static void main(String[] args) {

            ArrayList<ReadFile> fileList = new ArrayList<ReadFile> ();

            try (BufferedReader br = new BufferedReader(new FileReader("C:\\tagNo.txt")))
            {

                String CurrentLine;

                while ((CurrentLine = br.readLine()) != null) {
                    System.out.println(CurrentLine);


                    fileList.add(CurrentLine);

                }

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

        }
    }

I get an error at fileList.add(CurrentLine);. I know this is because CurrentLine does not match with ReadFile.

How can I make this work?

Thank you.

2
  • filelist is an array of readfiles, while currentline is a string. so i guess you have to create a new readfile object from a string. this obvioulsy depends on how the readfile is written in the file... Commented Feb 18, 2014 at 11:33
  • fileList should be a list of String. Commented Feb 18, 2014 at 11:33

8 Answers 8

3

The parameters in the < ... > brackets specify the type of the elements stored in the list. In this case, you want to store String objects. So it should be

ArrayList<String> fileList = new ArrayList<String> ();
Sign up to request clarification or add additional context in comments.

1 Comment

If you are using Java 7, then you can spare some typing and use the diamond inference, ArrayList<String> fileList = new ArrayList<> ();
1

You could replace

ArrayList <ReadFile> fileList = new ArrayList<ReadFile> ();

by

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

in JDK 7 style

Comments

0

You declared an arraylist that stores ReadFile but you want to add a String to the list which causes this exception. Create another List from type List<String> and store the file content there.

Comments

0

Try ArrayList<String> fileList instead of ArrayList<ReadFile>

Comments

0

Type of CurrentLine is String, fileList should be:

ArrayList<String> fileList = new ArrayList<String>();

Comments

0

Your arrayList is of type ReadFile. So it expects you to add elements of type ReadFile. What you need is change it to String.

 ArrayList<ReadFile> fileList = new ArrayList<ReadFile> ();

to

 ArrayList<String> fileList = new ArrayList<String> ();

Comments

0

Your ArrayList can hold objects of type ReadFile only, since you declared your ArrayList using <ReadLine>

 Change your ReadFile class to something like 

Class ReadFile {

  String line;
  ReadFile(String currentLine){
     this.line = currentLine;
  }
}

Then you can do

fileList.add(new ReadFile(CurrentLine));

Comments

0

You have to provide String instead of readfile. because here your list is expecting readfile.

ArrayList list = new ArrayList();

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.