Search code examples
javaif-statementhashmapiteratorlinkedhashset

java.util.LinkedHashMap$LinkedKeyIterator infinite loop in LinkedHashSet


System.out.print("Enter the size of linked hash set: ");
    Scanner s = new Scanner(System.in);  // Create a Scanner object
    int size = s.nextInt();
        HashSet<String> lhashset = new HashSet<>(size);
    for(int i=0; i<size; i++)
    {
        System.out.print("Enter the name: ");
        String name = s.next();
        lhashset.add(name);
    }
        System.out.println("\nEnter the name you want to find: ");
        String find = s.next();
        if(lhashset.contains(find))
        {       
            System.out.println("\nYes the Linked Hash Set contains " +find);
            System.out.print("Do you want to remove the name? ");
            String ch = s.next();
        String choice = "yes";
            if(ch.equals(choice))
            {
                    lhashset.remove(find);
                    System.out.println("\nElement removed.");
            }
            else
            {
                    System.out.println("\nGoodbye");
            }
        }
        else
        {
            System.out.println("Does not contain name.");
        }

The first if statement works fine, but when I try to go to else statement(print"does not contain"), I get an infinite loop as per the heading. The same happens for the nested if statement.


Solution

  • ch.equals(choice) will not work. Updating the code with correct syntax.

    public class Soln {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);  // Create a Scanner object
        HashSet<String> lhashset = new HashSet<>();
        lhashset.add("TEST");
        System.out.println("\nEnter the name you want to find: ");
        String find = s.next();
        if(lhashset.contains(find))
        {       
            System.out.println("\nYes the Linked Hash Set contains " +find);
            System.out.print("Do you want to remove the name? ");
            int ch = s.nextInt();
            if(ch == 1)
            {
                lhashset.remove(find);
                System.out.println("\nElement removed.");
            }
            else
            {
                System.out.println("\nGoodbye");
            }
        }
        else
        {
            System.out.println("Does not contain name.");
        }
    }
    }