John Joe
John Joe

Reputation: 12803

List always one [JAVA]

I'm trying to read text file in JAVA, retrieve some value and do the query. It should has two data in list, but it only show one.

BufferedReader br = new BufferedReader(new FileReader(file));
String st;
List<Object[]> list = null;

try {
    while ((st = br.readLine()) != null) {

        String id = st.substring(92, 100); // get from text file
        System.out.println(id);                

        list = commDAO.getDetails(id);  // query
        System.out.println("Total list " + list.size());
    }

    br.close();
} catch (Exception e) {
      e.printStackTrace();
}

logger.printInfo(AAA.class, "**** retrieve list: " + list.size());

Output

1
Total list 1
2
Total  list 1
retrieve list: 1

getDeails

  public List getDetails(String id) {
          .......
 return em.createQuery(bf.toString())
          .......
     .getResultList();
 }

I create another list and add the list to TotalList

List TotalList = new ArrayList();

list = TotalList.add(id);

But get incompatible types: boolean cannot be converted to List<Object[]>

Upvotes: 0

Views: 92

Answers (3)

John Joe
John Joe

Reputation: 12803

This is my solution

List<Object[]> list = new ArrayList<>();

         try {
              while ((st = br.readLine()) != null) {
                  String id = st.substring(92, 100); // get from text file
                  System.out.println(id);                
                  list.addAll(commDAO.getDetails(id));  // query
                 }
              System.out.println("Total list " + list.size());
        } catch (Exception ex) {
       }

getDetails

 public List<Object[]> getDetails(String id) {
          .......
 return em.createQuery(bf.toString())
          .......
     .getResultList();
 }

Upvotes: 0

Haruki
Haruki

Reputation: 109

If i get it right problem is that you always assign new value to the list instead of adding new one to it. You have:

list = commDAO.getDetails(id);

You should have:

list.add(commDAO.getDetails(id));

Upvotes: 1

Mathias Bader
Mathias Bader

Reputation: 3826

What does this line do? What is the object commDAO?

list = commDAO.getDetails(id);

It looks like you are overwriting the list object with a completely new list instead of adding an object to the existing list.

Try the following:

list.add(commDAO.getDetails(id));

or if commDAO.getDetails(id) returns a list, the following:

list.add(commDAO.getDetails(id).get(0));

Upvotes: 1

Related Questions