unknown
unknown

Reputation: 397

Duplicate value in array and no duplicate value found

I've a program that if I enter a number the program will trace the duplicate value , and it works fine. It traces the value whether there is any duplicate and it also displays the duplicated value.

What if I enter a number that has no duplicate how do I will inform the users that there is no duplicated value in the array.

Here is the code.

case "Search Duplicate Value":
{   
    search = (String) JOptionPane.showInputDialog(null , "Search Duplicate Value" , "Search" , JOptionPane.INFORMATION_MESSAGE);
    int parseSearchDuplicate = Integer.parseInt(search);

    for(int i = 0; i <= arrayElements.length ; i++)
    {               
        if(arrayElements[i] == parseSearchDuplicate && i == parseSearchDuplicate)
        {
            equalisStringDuplicate = equalisStringDuplicate + " " + arrayElements[i];                   
        }   
    }           
}
JOptionPane.showMessageDialog(null , new Label(equalisStringDuplicate + " "),  "Duplicate Value is:" , JOptionPane.INFORMATION_MESSAGE);                                
equalisStringDuplicate = "";
break;  

Upvotes: 0

Views: 69

Answers (1)

Sarfaraz Khan
Sarfaraz Khan

Reputation: 2186

Use a boolean value and set it to true if there is match and depending on the value show user a message .try below

case "Search Duplicate Value":
        {   
        search = (String) JOptionPane.showInputDialog(null , "Search Duplicate Value" , "Search" , JOptionPane.INFORMATION_MESSAGE);
            int parseSearchDuplicate = Integer.parseInt(search);
            boolean isduplicate=false;
            for(int i = 0; i < arrayElements.length ; i++)
            {               
                if(arrayElements[i] == parseSearchDuplicate && i == parseSearchDuplicate)
                {
                    isduplicate=true;
                    equalisStringDuplicate = equalisStringDuplicate + " " + arrayElements[i];                   
                }   
            }           
        }
        If(isduplicate)
JOptionPane.showMessageDialog(null , new Label(equalisStringDuplicate + " "),  "Duplicate Value is:" , JOptionPane.INFORMATION_MESSAGE);                                
  else 
      //show no duplicate found
        equalisStringDuplicate = "";
        break;  

Upvotes: 2

Related Questions