简体   繁体   中英

java - avoid outputting first line that is inputted by user

So I'm trying to create a program that reads in user input and outputs the current line only if it is smaller than some previous line. My program works fine but it keeps printing the first line I input which shouldn't be valid because there is no other line to compare it to yet.

I was wondering if there is a way for the program to consider the first line but not output it to the user? Thanks for any help!

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    ArrayList<String> strings = new ArrayList<>();
    String line;
    while((line = r.readLine()) != null) {
        boolean checkIfSmaller = true;
        strings.add(line);
        for (int i = 0; i < strings.size()-1; i++) {
            if (line.compareTo(strings.get(i)) >= 0) {
                checkIfSmaller = false;
            }
        }

        if (checkIfSmaller) {
            w.println(line);
        }
    }
}

The idea is do add the line to the list after checking for smaller lines, since it doesn't make sense to compare a line to itself. Also, the compare you make is not right IMO:

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    ArrayList<String> strings = new ArrayList<>();
    String line;
    while((line = r.readLine()) != null) {
        boolean isSmaller = false;
        for (int i = 0; i < strings.size()-1; i++) {
            if (line.compareTo(strings.get(i)) < 0) {
                isSmaller = true;
                break;
            }
        }
        strings.add(line);
        if (isSmaller) {
            w.println(line);
        }
    }
}

Try the below code,

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    ArrayList<String> strings = new ArrayList<>();
    String line;
    boolean checkIfSmaller = false;
    while((line = r.readLine()) != null) {
        if (checkIfSmaller) {
            w.println(line);
        }
        checkIfSmaller = false;
        strings.add(line);
        for (int i = 0; i < strings.size()-1; i++) {
            if (strings.get(i).compareTo(line) >= 0) {
                checkIfSmaller = true;
            }
        }
    }
}

You don't have to maintain a list of all the previous strings. Only maintaining the smallest string you have encountered so far should be enough. Based on this logic, code can be improvised as follow

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
        String line;
        String smallestLineSoFar = null;
        while ((line = r.readLine()) != null) {
            if (smallestLineSoFar == null) {
                // this case is for first line
                smallestLineSoFar = line;
            } else if (line.compareTo(smallestLineSoFar) < 0) {
                smallestLineSoFar = line;
                w.println(line);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM