someangular someangular - 4 years ago 153
Java Question

cannot convert from Integer to Integer []

I have two integer variables nbBooks and libraryId.

I am trying to print the library id and the total number of books in that library in one line.

My ideal is to get both of these variables as integers and do a loop through both, but currently I am getting nbBooks as

String
and then converting it back to integer and passing it to
NBBooks
which i suppose is redundant.

and trying at the same time to get the libraryId as integer, but I get:
cannot convert from Integer to Integer []
.

Question: could somebody help me to print library id and total book number in one line through the loop?

String[] nbBooks = request.getParameterValues("nbBooks");
Integer[] libraryId = Integer.valueOf(request.getParameter("libraryId"));

for (int i = 0; i < nbBooks.length; i++) {
int NBBooks = Integer.parseInt(nbBooks[i]);
int id = libraryId [i];
System.out.println("library id is: "+ id + " total number of books is: "+ NBBooks);
}


a note update:



libraryId is the id of the library, and nbBooks is the total number of books available in that library.

Answer Source

If libraryId is String[]

String[] nbBooks = request.getParameterValues("nbBooks");
String[] libraryId = request.getParameterValues("libraryId");

for (int i = 0; i < nbBooks.length; i++) {
   int totalBooks = Integer.parseInt(nbBooks[i]);
   int id = Integer.parseInt(libraryId [i]);
   System.out.println("library id is: "+ id + "  total number of books is: "+ totalBooks);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download