0

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();
}

}

2
  • 3
    Can you share the error you are getting Commented Jul 28, 2022 at 4:27
  • Thanks for the feedback Kleo. I'm still learning the basics but I will addressed. Commented Jul 28, 2022 at 16:13

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank youuuuu. Now runs perfectly, it was just a simple error haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.