java - 读取输入行并输出重复项

标签 java text line

所以我正在学习如何读取文本文件等,并且我正在尝试制作一个程序,一次读取一行输入并输出当前行当且仅当它小于读取的任何其他行时远的。较小是相对于字符串的通常顺序而言的,如 String.compareTo() 所定义。

当我尝试运行代码时,出现错误“List 无法解析为类型”和“ArrayList 无法解析为类型”。我很困惑为什么我的程序中出现此错误,并且想知道我是否应该使用其他东西?

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);
        }
    }
}

最佳答案

您可以只使用整数作为最小长度:

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);
        }
    }
}

输入文件:

1111111
222222222
3333333333
444

输出:

1111111
444

您的代码还有以下错误:

    List<String> allStrings = new ArrayList<>();

 // It throws IndexOutOfBoundsException!!!
    String shortest = allStrings.get(0);

关于java - 读取输入行并输出重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58037361/

相关文章:

java - android studio 找不到 "Activity"符号

java - 创建透明面板

java - asp classic sql server 2005无法连接

android - 在 MPAndroidChart 的某些情况下管理文本 PieChart

ios - 调整文本大小以适应 Swift 中的标签

excel - 当我计算文本的行数时,为什么结果为 1?

java - 改造 call.enqueue 代码 200,但 response.body() 为空

iOS - 按标签查找和操作文本字段?

c - 逐行读取csv文件并输出第五列用于在c中匹配

android - 如何在运行时在自定义 View 中画线?