java .util.LinkedHashMap $ LinkedKeyIterator бесконечный l oop в LinkedHashSet - PullRequest
1 голос
/ 05 апреля 2020
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.");
        }

Первый оператор if работает нормально, но когда я пытаюсь go в оператор else (вывести «не содержит»), я получаю бесконечное значение l oop в соответствии с заголовком. То же самое происходит с вложенным оператором if.

1 Ответ

0 голосов
/ 05 апреля 2020

ch.equals (выбор) не будет работать. Обновление кода с правильным синтаксисом.

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.");
    }
}
}
...