Dipesh Shrestha
Dipesh Shrestha

Reputation: 57

How to fix error using equalsIgnorecase in java netbeans?

I am building java project in inventory management. following is the code i used for inserting color in database using equalsIgnorecase but it continuous showing Already exist. Please some one fix my code. thanks

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        if(txtNewColor.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Fields should not be empty");
        }
        else
        {

            try {

                String c = txtNewColor.getText();
                ps =DbConnection.cn.prepareStatement("Select Color from color_details");
                rs = ps.executeQuery();
                int color = 0;
                while (rs.next())
                {
                    String cl= rs.getString("Color");
                    if(cl.equalsIgnoreCase(cl));

                    {
                        JOptionPane.showMessageDialog(null, "Aready Exist");
                        txtNewColor.setText("");
                        color=1;
                    }
                }

                if (color==0)
                {

                    String strdata="Insert into color_details (Color)values(?)";
                    ps=DbConnection.cn.prepareStatement(strdata);

                    ps.setString(1, txtNewColor.getText());
                    ps.execute();

                    JOptionPane.showMessageDialog(null, "New Color Added Successfully");
                    cleartext();

                }
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, e);
            }
        }
        refreshTable();

        }

Upvotes: 0

Views: 1003

Answers (3)

Bruce
Bruce

Reputation: 8849

You used same two strings to compare. so change c.equalsIgnoreCase(c1). Also make sure you have removed trailing spaces when getting input from text fields. it may makes your comparison fail.

String c = txtNewColor.getText().trim();

Remove the semi colon after if clause if(cl.equalsIgnoreCase(cl)); ---> if(cl.equalsIgnoreCase(cl))

Upvotes: 1

Uma Kanth
Uma Kanth

Reputation: 5629

You are comparing the same String again. So It always results in a true, also the ; will skip even if they match. Remove it.

 String c = txtNewColor.getText();
                ps =DbConnection.cn.prepareStatement("Select Color from color_details");
                rs = ps.executeQuery();
                int color = 0;
                while (rs.next())
                {
                    String cl= rs.getString("Color");
                    if(cl.equalsIgnoreCase(c))
                    {
                        JOptionPane.showMessageDialog(null, "Aready Exist");
                        txtNewColor.setText("");
                        color=1;
                    }
                }

Upvotes: 1

Phuthib
Phuthib

Reputation: 1436

Try change if(cl.equalsIgnoreCase(cl)); to if(c.equalsIgnoreCase(cl))

Had not spotted the semi-colon at the end of your if statement

Upvotes: 1

Related Questions