java - Loop over 2 lists and print the result to a csv -


i have issue in trying loop on 2 lists (of different lengths) , print result csv.

below code:(i have marked line need help)

@requestmapping(value = "/do.download")     public string download(form form, model model,httpservletrequest request, httpservletresponse response)throws ioexception {         printwriter pw = null;         try {             logger.debug("+++++++++++++++++++++++++++++++++++++++");             response.setcontenttype("application/vnd.ms-excel");             response.setheader("content-disposition","attachment; filename=" + "backup"+ ".csv");             response.setheader("expires", "0");             response.setheader("cache-control","must-revalidate, post-csheck=0, pre-check=0");             response.setheader("pragma", "public");             pw = response.getwriter();              pw.println("carid,carname,carmodel,carcolor");              car carfromdb = new car();              list<cardetails> cardetailsfromdb = collections.emptylist();             list<carcolor> carcolorfromdb = collections.empltylist();               cardetailsfromdb = service.getcardetails(carfromdb);             carcolorfromdb =  service.getcarcolor(carfromdb);               (cardetails cardetails : cardetailsfromdb) {                  pw.println(string.format("%s,%s,%s,%s",                          cardetails.getcarid(),                         cardetails.getcarname(),                         cardetails.getcarmodel(),                         //get car color listcarcolorfromdb has carid <--- need                           ));             }             pw.flush();          } catch (exception ex) {             ex.printstacktrace();         }         return null;     } 

i'm not sure if can have multiple colors per car. if so, below can see can loop through colors inside other loop , compare carid's. if match, add color string. use created string last parameter println. fixed formatting issues names , capitalization. classes should upper case , variables lower case.

@requestmapping(value = "/do.download") public string download(form form, model model,httpservletrequest request, httpservletresponse response)throws ioexception {     printwriter pw = null;     try {         logger.debug("+++++++++++++++++++++++++++++++++++++++");         response.setcontenttype("application/vnd.ms-excel");         response.setheader("content-disposition","attachment; filename=" + "backup"+ ".csv");         response.setheader("expires", "0");         response.setheader("cache-control","must-revalidate, post-csheck=0, pre-check=0");         response.setheader("pragma", "public");         pw = response.getwriter();          pw.println("carid,carname,carmodel,carcolor");          car carfromdb = new car();          list<cardetails> cardetailsfromdb = new arraylist<cardetails>();         list<carcolor> carcolorfromdb = new arraylist<carcolor>();           cardetailsfromdb = service.getcardetails(carfromdb);         carcolorfromdb =  service.getcarcolor(carfromdb);           (cardetails cardetails : cardetailsfromdb) {              //loop through colors , find ones match carid cardetails             // concatenate them "|"             string colorsstr = "";             for(carcolor color : carcolorfromdb){                if(color.getcarid() == cardetails.getcarid()){                    colorsstr = colorsstr + "|" + color.tostring();                }             }             //chop off first character should "|"              colorsstr = colorsstring.substring(1);               pw.println(string.format("%s,%s,%s,%s",                      cardetails.getcarid(),                     cardetails.getcarname(),                     cardetails.getcarmodel(),                     //get car color listcarcolorfromdb has carid <--- need                      colorsstr                     ));         }         pw.flush();      } catch (exception ex) {         ex.printstacktrace();     }     return null; } 

Comments