-1
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
Alex
  • 39
  • 9
Pratik Paul
  • 93
  • 2
  • 12
  • 1
    Your codes works fine for me. Just place the last loop outside of the the outer loop to print the results after the completion of the insertion. *Friendly reminder*: fix your indentation! – Lefteris008 May 11 '16 at 07:50
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald May 11 '16 at 08:49

1 Answers1

0

Place your print loop outside of input loop:

for(int j = 0; j < i; j++) {
    String s = scr.nextLine();
    if (s.equals(">")) {
        break;
    } else {
        hs.add(s);
    }
}

for (String str : hs) {// print loop
    System.out.println("The Entries are " + str);
}
rev_dihazum
  • 818
  • 1
  • 9
  • 19