Search code examples
javatextline

java - reading input lines and outputting duplicates


So I'm learning about reading textfiles and such, and I'm trying to make a program that reads the input one line at a time and outputs the current line if and only if it is smaller than any other line read so far. Smaller is with respect to the usual order on Strings, as defined by String.compareTo().

When I try to run my code, I get errors "List cannot be resolved to a type" and "ArrayList can not be resolved to a type". I'm confused as to why I got this error in my program, and was wondering if there is something else I'm supposed to use?

package comp;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;

public class Part2 {

    public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
        List<String> allStrings = new ArrayList<>();

        String line;
        String shortest = allStrings.get(0);
        while((line = r.readLine()) != null) {
            for(String s: allStrings) {
                if(s.length()>shortest.length()) {
                    shortest = s;
                    line = shortest;
                }
            }
            allStrings.add(line);

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

    public static void main(String[] args) {
        try {
            BufferedReader r;
            PrintWriter w;
            if (args.length == 0) {
                r = new BufferedReader(new InputStreamReader(System.in));
                w = new PrintWriter(System.out);
            } else if (args.length == 1) {
                r = new BufferedReader(new FileReader(args[0]));
                w = new PrintWriter(System.out);                
            } else {
                r = new BufferedReader(new FileReader(args[0]));
                w = new PrintWriter(new FileWriter(args[1]));
            }
            long start = System.nanoTime();
            doIt(r, w);
            w.flush();
            long stop = System.nanoTime();
            System.out.println("Execution time: " + 10e-9 * (stop-start));
        } catch (IOException e) {
            System.err.println(e);
            System.exit(-1);
        }
    }
}

Solution

  • You could just use integer for min length:

    public class Part2 {
    
        public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
            List<String> allStrings = new ArrayList<String>();
            String line;
            int minLength = 0;
            while ((line = r.readLine()) != null) {
                if (minLength == 0 || minLength > line.length()) {
                    allStrings.add(line);
                    minLength = line.length();
                }
            }
    
            for (String text : allStrings) {
                w.println(text);
            }
        }
    
        public static void main(String[] args) {
            try {
                BufferedReader r;
                PrintWriter w;
                if (args.length == 0) {
                    r = new BufferedReader(new InputStreamReader(System.in));
                    w = new PrintWriter(System.out);
                } else if (args.length == 1) {
                    r = new BufferedReader(new FileReader(args[0]));
                    w = new PrintWriter(System.out);
                } else {
                    r = new BufferedReader(new FileReader(args[0]));
                    w = new PrintWriter(new FileWriter(args[1]));
                }
                long start = System.nanoTime();
                doIt(r, w);
                w.flush();
                long stop = System.nanoTime();
                System.out.println("Execution time: " + 10e-9 * (stop - start));
            } catch (IOException e) {
                System.err.println(e);
                System.exit(-1);
            }
        }
    }
    

    Input file:

    1111111
    222222222
    3333333333
    444
    

    Output:

    1111111
    444
    

    Also your code has following bug:

        List<String> allStrings = new ArrayList<>();
    
     // It throws IndexOutOfBoundsException!!!
        String shortest = allStrings.get(0);