-7

I want to list 70 albums, each one having album information(name, image, description).

In the Java class Im getting that 70 albums with the method getAlbums qm1.getAlbums. The result of this operation is on this format (first the names then the ids of the albums):

[[‘Achtung Baby’, …, ‘Pop’], [100, .., 160]] 

(achthunb baby has the id 100, and the Pop album has the id 160)

This part is working fine.

Now in the Java class is caled other method “qm.getAlbumInfo(name, id)” where I want for each returned album above return the information(name, image, description).

Error: But its not working properly it shows always the information of the first album three 3 times. So its not showing all the 70 albums, just shows 3 albums and all 3 albums that appear are the same.

Do you know where is the error?

Java class:

         QueryManager qm1 = new QueryManager();

        // qm1.getAlbums returns on this format
        // [[‘Achtung Baby’, …, ‘Pop’], [100, .., 160]] 
        ArrayList<ArrayList<String>> result1 = qm1.getAlbums();
        qm1.closeConnections();

        String name =  result1.get(0).toString();
        String id   =  result1.get(1).toString();

        QueryManager qm = new QueryManager();
        ArrayList<ArrayList<String>> result;
     // the error maybe is in this part:
       for (int i = 0; i<= result1.size(); i++)
            result = qm.getAlbumInfo(name[i], id[i]);
        qm.closeConnections();        
        
        ArrayList<String> albumInfo = result.get(0);

        System.out.println(albumInfo.size() + "albumInfo SIZE");  // shows 3
        System.out.println(result1.size() + "result1 SIZE"); // shows 2
        System.out.println(name.size() + "name SIZE"); // shows 70
        System.out.println(id.size() + "id SIZE"); // shows 70
Community
  • 1
  • 1
OzzyW
  • 117
  • 3
  • 14
  • Shouldn't you query from `item` variable inside the loop? – M. Prokhorov Dec 14 '17 at 16:48
  • The code puts `albumInfo` as the `result` used by the jsp. Is that correct? Debug and break on this line: `request.setAttribute("result", albumInfo);` to check the contents of `albumInfo`. Backtrack a few lines to see what's going on. – Andrew S Dec 14 '17 at 16:54
  • Thanks. albumInfo prints the info (name, image link, description) but only of the frist album. – OzzyW Dec 14 '17 at 17:06
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Dec 16 '17 at 17:19
  • there is no way this is the code you are running as it would fail on the following no matter what the size of the list `i<= result1.size()`. the proper way to work with data is to encapsulate it in `Objects` not lists of lists of lists of lists, that is just a recipe for disaster, which this code obviously is. –  Dec 16 '17 at 17:20
  • But it dont appear any error, just appears 3 albums and not 70 and the 3 albums are repeated. – OzzyW Dec 16 '17 at 17:46
  • 1
    This has got completely nothing to do with JSP/Servlets. This is just a basic Java mistake. Although your question is in its current form unanswerable as the code doesn't at all compile in first place. You seem to have edited it on the fly without testing. Please reframe your question to include a normal [mcve] in flavor of a plain Java class with main() method. Present the results using a plain Java for loop with System.out.println() instead of a whole JSP/Servlet web application interface. For future questions, please read [ask] as well to avoid common mistakes while preparing the question. – BalusC Dec 19 '17 at 08:08
  • You are expected to award the bounty to your accepted answer. – Cardinal System Dec 20 '17 at 21:54

1 Answers1

1

There is a LOT of problem in your code...

First, the loop :

ArrayList<ArrayList<String>> result;
// the error maybe is in this part:
for (int i = 0; i<= result1.size(); i++)
    result = qm.getAlbumInfo(name[i], id[i]);

You will only get the last qm.getAlbumInfo(name[i], id[i]) as a result.

You probably want to add every List<String> (if this is the return type of getAlbumInfo) into a List. Without that, you will not keep every result.

 List<<List<String>> result = new ArrayList<List<String>>();
 for (int i = 0; i<= result1.size(); i++)
    result.add(qm.getAlbumInfo(name[i], id[i]));

If am a bit worried about the condition of the loop, if you start at i = 0, you most likely need to check for i < result1.size, not i <= result1.size

But then, name[i] and id[i] can't compile since those are String.

The way you get those String are wrong too :

ArrayList<ArrayList<String>> result1 = qm1.getAlbums();
qm1.closeConnections();

String name =  result1.get(0).toString();
String id   =  result1.get(1).toString();

The return of result1.get is a ArrayList<String>, so I doubt tostring is what you expect.

From all that. I would guess that the actual code you want is :

List<List<String>> resultAlbum = qm1.getAlbums();

List<String> albumInfo;
String name, id;
List<<List<String>> resultInfo = new ArrayList<List<String>>();
for (int i = 0; i<= resultAlbum.size(); i++){
    //get on album row
    albumInfo = resultAlbum.get(i);

    name = albumInfo.get(0);
    id = albumInfo.get(1);

    //Get the info for this album and add into the list
    resultInfo .add(qm.getAlbumInfo(name, id));
}
AxelH
  • 14,325
  • 2
  • 25
  • 55