My case 3 does not delete from the list and case 4 does not compare and returns the amount of gender written

Asked

Viewed 25 times

0

I register some books, after that I try to delete some with my case 3, but typing case 2 to show all it continues showing the book that should have been deleted. My case 4 should compare the typed Gender String to the Gender String of the Book class of each list, but it always returns the ZERO value on my Count as if it had no String equal ... Follow my main code and the object below.

package application;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    import entities.Livro;

    public class Program {

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);
            Livro livroLivraria;
            List<Livro> livros = new ArrayList<>();

            int count = 0;

            System.out.println("1 - Cadastrar Livro\n2 - Listar \n3 - Excluir Livro\n4 - Pesquisar Livro pelo gênero\n"
                    + "5 - Pesquisar Livro por faixa de preço\n6 - Calcular Total do Acervo\n7 - Sair\n");

            int opcao = sc.nextInt();

            do {
                switch(opcao) {
                    case 1 :    System.out.print("Quantos livros quer cadastrar ? ");
                                int j = sc.nextInt();
                                sc.nextLine();
                                for(int i=1; i <= j; i++) {
                                    System.out.println("LIVRO " + i);
                                    System.out.print("Nome: ");
                                    String nome = sc.nextLine();
                                    System.out.print("Autor: ");
                                    String autor = sc.nextLine();
                                    System.out.print("Gênero: ");
                                    String genero = sc.nextLine();
                                    System.out.print("Preço: ");
                                    Double preco = sc.nextDouble();
                                    sc.nextLine();
                                    livroLivraria = new Livro(nome, autor, genero, preco);
                                    livros.add(livroLivraria);
                                }
                                break;

                    case 2 :    for (Livro l : livros) {
                                    System.out.println(l);
                                }
                                break;

                    case 3 :    System.out.print("Qual nome do livro que deseja excluir ? ");
                                String nome = sc.nextLine();
                                sc.nextLine();

                                for (Livro l : livros) {
                                    if (l.getNome().equals(nome)) {
                                        livros.remove(l);
                                    }
                                }

                                System.out.println("Livro excluído...");
                                break;

                    case 4 :    System.out.print("Qual gênero procura ? ");
                                String genero = sc.nextLine();
                                sc.nextLine();

                                count = 0;
                                for(Livro l : livros) {
                                    if (l.getGenero().equals(genero)) {
                                        count++;
                                    }
                                }
                                System.out.println(count + " livros do gênero " + genero);

                                break;

                    case 5 :    System.out.print("Digite o valor inicial: ");
                                double p1 = sc.nextDouble();
                                System.out.print("Digite o valor final: ");
                                double p2 = sc.nextDouble();

                                count = 0;
                                if (p1 < p2) {
                                    for(Livro l : livros) {
                                        if (l.getPreco() >= p1 && l.getPreco() <= p2) {
                                            count++;
                                        }
                                    }
                                    System.out.println(count + " livros entre os valores R$" + p1 + " e R$" + p2);
                                }else {
                                    System.out.println("ERRO: valor inicial maior que valor final...");
                                }

                                break;

                    case 6 :    double t=0;
                                for (Livro l : livros) {
                                    t += l.getPreco();
                                }
                                System.out.println("Valor total dos livros R$" + t);

                                break;

                    case 7 : break;

                    default :   System.out.println("Esta opção não existe...");
                                break;
                }

                System.out.println("\n1 - Cadastrar Livro\n2 - Listar \n3 - Excluir Livro\n4 - Pesquisar Livro pelo gênero\n"
                        + "5 - Pesquisar Livro por faixa de preço\n6 - Calcular Total do Acervo\n7 - Sair\n");

                opcao = sc.nextInt();

            }while(opcao != 7);

            sc.close(); 
        }
    }

Class Object Book:

package entities;

public class Livro {

    private String nome;
    private String autor;
    private String genero;
    private Double preco;

    public Livro(String nome, String autor, String genero, Double preco) {
        this.nome = nome;
        this.autor = autor;
        this.genero = genero;
        this.preco = preco;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getAutor() {
        return autor;
    }

    public void setAutor(String autor) {
        this.autor = autor;
    }

    public String getGenero() {
        return genero;
    }

    public void setGenero(String genero) {
        this.genero = genero;
    }

    public Double getPreco() {
        return preco;
    }

    public void setPreco(Double preco) {
        this.preco = preco;
    }

    @Override
    public String toString() {
        return "Livro [ Nome = " + nome + ", Autor = " + autor + ", Genero = " + genero + ", Preço = " + preco + " ]";
    }

}

How would I solve this ?

1 answer

0


What happens is that in your Case 3, in the stretch:

System.out.print("Qual nome do livro que deseja excluir ? ");
                                String nome = sc.nextLine();
                                sc.nextLine();

You call sc.nextLine() to the String name, then you cancel by calling sc.nextLine() again. Just by reversing the calls, the problem no longer occurs. It is the same case for the other cases.

System.out.print("Qual nome do livro que deseja excluir ? ");
                sc.nextLine();
                String nome = sc.nextLine();

Take a look at the Scanner class and immutability.

I hope I’ve helped.

  • It worked in case 4, but 3 gives the following error after I type the name of the book I want to delete: Which name of the book do you want to delete ? HP2 Exception in thread "main" java.util.Concurrentmodificationexception at java.base/java.util.Arraylist$Itr.checkForComodification(Arraylist.java:1042) at java.base/java.util.Arraylist$Itr.next(Arraylist.java:996) at application.Program.main(Program.java:54)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.