1
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.

1 Answers1

0

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.");
    }
}
}
Bhawna Joshi
  • 190
  • 2
  • 12
  • same problem persist when I try to go to the else statement and nested if statement. I get an infinite loop whenever I enter a name not contained in the set and whenever I type 1 when asked. – UJJWAL VERMA Apr 06 '20 at 05:18
  • What are your inputs?Can you share your complete code – Bhawna Joshi Apr 06 '20 at 05:40
  • Test Case 1 : Inputs : 2 Ujjwal Verma Shyam and then it shows the error. – UJJWAL VERMA Apr 06 '20 at 08:50
  • Test Case 2 : inputs : 2; Ujjwal; Verma; Ujjwal; yes; and then it shows the error. Test Case 3 : Ujjwal; Verma; Ujjwal; no; and then it shows the error. – UJJWAL VERMA Apr 06 '20 at 08:51
  • @UJJWALVERMA you are inputing multi space character as an input in 1 case and s.next() will take only first word as input when loop is run for 1st time. It takes Verma in second time. I ran the program at my end and all cases were working fine – Bhawna Joshi Apr 07 '20 at 16:32
  • It worked, thanks. But what if I want to enter full names. I tried using `nextLine()` and when I print the set it shows `Here is the Linked Hash Set: [, Ujjwal Verma]`. Sample Code: `for(int i=0; i – UJJWAL VERMA Apr 07 '20 at 18:05