kevin-wayne/algs4

It does not work as expected. The input cannot be terminated.

Closed this issue · 2 comments

while (!StdIn.isEmpty()) {

You've messed things up somehow inside StdIn class (possibly with the Patterns used in the java.util.Scanner object).
The best recommended practice for using a java.util.Scanner in this use case would be AND is, the following:

...
    public static void main(String[] args)
    {
        Bag<String> bag = new Bag<String>();
        Scanner sc = new Scanner(new BufferedInputStream(System.in), "UTF-8");
        String item;
        
        while (sc.hasNextLine()) // OR even "true"
        {
            if (!(item = sc.nextLine()).equals("")) // Just press ENTER without typing anything in order to terminate input !!!
                bag.add(item);
            else
                break;
        }

        StdOut.println("size of bag = " + test.size());
        for (String s : test)
            StdOut.println(s);
    }
...

An example output is:

Ena
Dyo
Tria
Tessera
Hello from Greece <3 !!!

size of bag = 5
Hello from Greece <3 !!!
Tessera
Tria
Dyo
Ena

Closing. Standard input is terminated in terminal via Ctrl-D or Ctrl-Z (depending on OS). This is not the same thing as a blank line.