import java.util.HashSet;
import java.util.Scanner;
public class HashSetExample {
public static void main(String[] args)
{
System.out.println("Enter the number of Entries");
Scanner sc =new Scanner(System.in);
int i=sc.nextInt();
HashSet<String> hs=new HashSet<String>();
System.out.println("Enter the Entries, > to quit");
Scanner scr =new Scanner(System.in);
for(int j=0;j<i;j++) {
String s=scr.nextLine();
if (s.equals(">")) {
break;
} else {
hs.add(s);
}
for(String str:hs) {
System.out.println("The Entries are" + str);
}
}
}
In the above program, at first the user is required to enter the number of entries. If the user enters 10 then the console should prompt for the Entries ten times. If in case after entering 5 entries the user presses the > button, the program should terminate and show the entries entered so far. But due to some logical error the program terminates after entering the first entry and displays the first entry only. The output is:
Enter the number of Entries
10
Enter the Entries, > to quit
Paul
The Entries arePaul