-4

this is my first topic, i'm pt-br, so beforehand i will apologize for my english.

Here's my question, i have this code, i type the id to look in the ArrayList if there are equals, if positive, it brings to the screen the registered item, if negative, it goes to the else.

But in this code, it's going to the else even when there are a registered item. It shows the registered item and then the else block code is executed.

I don't know what's going on, for me it's correct.

//abre a opção para o usuário digitar o id para a busca
int opcao = Integer.parseInt(JOptionPane.showInputDialog("Digite o ID para a busca"));
//'for' para percorrer o vetor
for (Produto objProduto : vetorProdutos2) {
    //if para verificar se o ID digitado para busca contém no vetor
    if (objProduto.getId() == opcao) {
        JOptionPane.showMessageDialog(null,
            "\nID: " + objProduto.getId() + "\nDescrição: " + objProduto.getDescricao()
                + "\nEstoque: " + objProduto.getEstoque() + "\nPreço: "
                + objProduto.getPreço() + "\nStatus: " + objProduto.getStatus());
    } else if (objProduto.getId() != opcao) {
        JOptionPane.showMessageDialog(null, "Produto não encontrado");
    }
}                   
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
KaioMartins
  • 115
  • 1
  • 9

3 Answers3

1

The problem is that you keep searching until the end of the list. So, even if you found a matching item, the next elements may be different from the one you seek, and therefore the else would be true. What you need to do, is display the "not found" message only if no matching element was found: Use a boolean flag found like this:

    int opcao = Integer.parseInt(JOptionPane.showInputDialog("Digite o ID para a busca"));
        boolean found = false;

        //'for' para percorrer o vetor
        for (Produto objProduto : vetorProdutos2) {
         //if para verificar se o ID digitado para busca contém no vetor
         if (objProduto.getId() == opcao) {
                     JOptionPane.showMessageDialog(null,
      "\nID: " + objProduto.getId()+"\nDescrição: " + objProduto.getDescricao()
                                                    + "\nEstoque: " + objProduto.getEstoque() + "\nPreço: "
                                                    + objProduto.getPreço() + "\nStatus: " + objProduto.getStatus());
                   found = true;
               } 
         }
 if (! found)
 {
  JOptionPane.showMessageDialog(null, "Produto não encontrado");
}
aviad
  • 1,553
  • 12
  • 15
0

Remove the last else and replace it with a default statemwnt that you want to display if all options fail. At the end of your else if put the JOptionPane.showMessage..... to show your default message.

-2

I'm not sure what's going wrong in your code (it could be that one of the .get functions is modifying the objProduto in a hidden way).

In either case, this may solve your problem, and it is logically the same:

//abre a opção para o usuário digitar o id para a busca
                int opcao = Integer.parseInt(JOptionPane.showInputDialog("Digite o ID para a busca"));
                //'for' para percorrer o vetor
                for (Produto objProduto : vetorProdutos2) {
                    //if para verificar se o ID digitado para busca contém no vetor
                    if (objProduto.getId() == opcao) {
                        JOptionPane.showMessageDialog(null,
                                "\nID: " + objProduto.getId() + "\nDescrição: " + objProduto.getDescricao()
                                        + "\nEstoque: " + objProduto.getEstoque() + "\nPreço: "
                                        + objProduto.getPreço() + "\nStatus: " + objProduto.getStatus());
                    } else {
                        JOptionPane.showMessageDialog(null, "Produto não encontrado");
                    }
                }
Grumblesaurus
  • 3,021
  • 3
  • 31
  • 61
  • 2
    Yes, it was a redundant `else if` but how "may" this solve their problem? – ChiefTwoPencils May 07 '16 at 21:10
  • @ChiefTwoPencils"Yes, it was a redundant else if but how "may" this solve their problem?" I included the answer to that in my post. "t could be that one of the .get functions is modifying the objProduto in a hidden way". Use your reading skills. – Grumblesaurus May 08 '16 at 03:54
  • Don't be rude, especially when you haven't, as you say, included the answer to that. I asked you how your answer "***may***" solve their problem. What you have in your post and snarky comment is what may be ***wrong*** with their code. – ChiefTwoPencils May 08 '16 at 04:29