Search code examples
javabufferreadline

java - output text in case in-sensitive order


So I'm new to java and I am currently learning about how to read textfiles. I'm trying to construct a program that reads input one line at a time from the user, and when I press ctrl + z, it should output all the lines in case-insensitive sorted order. I'm a little confused about how to use collections and I tried to follow a similar example I found online. However, when I run my program, it just outputs whatever I entered without sorting anything. What am I doing wrong?

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    List<String> listStrings = new ArrayList<>();
    String line;
    while((line = r.readLine()) != null) {
        listStrings.add(line);
    }

    Collections.sort(listStrings);

    Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER);

    Collections.sort(listStrings, Collections.reverseOrder());

    Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER);

//  Collections.reverse(listStrings);

    for (String text: listStrings) {
        w.println(text);
    }
}

Solution

  • None of your Collections._____() invocations will print anything. They just operate on the underlying collection (listStrings). So, here's what you expect to end up with, after each step:

    //listStrings
    Collections.sort(listStrings);
    //listStrings sorted alphabetically, case sensitive
    Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER);
    //listStrings sorted alphabetically, case insensitive
    Collections.sort(listStrings, Collections.reverseOrder());
    //listStrings sorted alphabetically in reverse order, case insensitive
    Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER);
    //listStrings sorted alphabetically, case insensitive
    Collections.reverse(listStrings);
    //listStrings sorted alphabetically in reverse order, case insensitive
    

    Finally, after making all these changes to listStrings, you attempt to print the collection. The problem you run into here, is that you don't actually flush the output stream, which is likely buffered. So, nothing will be printed. I've rewritten your code to have the exact same effect on listStrings, and print the output, like so:

    public static void doIt(BufferedReader r, PrintWriter w) throws IOException
    {
        List<String> listStrings = new ArrayList<>();
        String line;
        while((line = r.readLine()) != null)
        {
            listStrings.add(line);
        }
    
        Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER.reversed());
    
        for(String text : listStrings)
        {
            w.println(text);
        }
        w.flush();
    }
    

    I call it from my main method like so:

    public static void main(String[] args) throws Exception
    {
        doIt(new BufferedReader(new InputStreamReader(System.in)), new PrintWriter(System.out));
    }
    

    Here is the resulting effect:
    Input:

    ABCD
    bcde
    fegh
    ijkl
    

    Output:

    ijkl
    fegh
    bcde
    ABCD