I hope you are having an amazing day.
I was practicing my POO on Java and I trying to make a program in which I register books info and them I calculated the book with the highest page number.
So, I sharing my algorithm with you. When I run this code, I get an error. I reviewed the ArrayList Library documentation and I was unable to find any solution.
Can you help me out with this guys? I'm a spanish speaker, so variables names are on my language. I sorry for that, but I pretty sure that you can understand it.
public static void registrarLibros() {
int cantidadLibros, numPag;
String ISBN, Titulo, Autor;
System.out.print("Ingrese la cantidad de libros a registrar: ");
cantidadLibros = scan.nextInt();
for (int i=0; i>cantidadLibros; i ) {
System.out.print("ISBN del libro " i ": ");
ISBN = scan.nextLine();
System.out.print("Titulo del libro " i ": ");
Titulo = scan.nextLine();
System.out.print("Autor del libro " i ": ");
Autor = scan.nextLine();
System.out.print("Cantidad de paginas del libro " i ": ");
numPag = scan.nextInt();
Libro libro = new Libro(ISBN, Titulo, Autor, numPag) {};
libros.add(libro);
}
}
public static void mostrarDatos() {
for (Libro l: libros) {
System.out.println(l.toString());
}
}
public static void libroMayorPaginas() {
int maxPag = 0;
String maxTitulo;
for (Libro l: libros)
{
if (maxPag<l.getNumeroPaginas())
{
maxPag = l.getNumeroPaginas();
maxTitulo = l.getAutor();
}
}
System.out.print("El libro con mayor cantidad de paginas es " maxTitulo " con " maxPag " paginas.");
}
public static void main(String[] args) {
//Registrar libros
registrarLibros();
//Mostrar datos
mostrarDatos();
//Calcular el libro con mayor cantidad de paginas
libroMayorPaginas();
}
}
CodePudding user response:
Inside the registrar libros method.I think the for loop should be
for(int i=0;i<cantidadLibros;i )
.But you have put the condition as i>catidadLibros
with i increment.so if i is already greater than catidadLibros it will become a endless loop or if it is lesser than,the for loop will not be executed