【问题标题】:java - reading input lines and outputting duplicatesjava - 读取输入行并输出重复项
【发布时间】:2019-09-21 04:49:20
【问题描述】:

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

当我尝试运行我的代码时,我收到错误“列表无法解析为类型”和“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);
        }
    }
}

【问题讨论】:

  • 你能发布你的全班吗?我在 doIt 中看到了错误,但我想在证明答案之前调试代码。

标签: java text line


【解决方案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);

【讨论】:

    【解决方案2】:

    这里有几个问题。如果你运行你的代码,你会在doIt() 中得到一个IndexOutOfBoundException,因为你的allStrings 列表是空的,因此当你调用String shortest = allStrings.get(0); 时,你调用的东西不在你的列表中。

    此外,if(s.length()&gt;shortest.length())) 的逻辑有点奇怪,因为本质上您实际上只是比较列表中的字符串,而不是与您从文件中读取的内容进行比较。因此,您永远无法确定您从文件中读取的行是否比您已有的任何行

    最后,这段代码……for (String text: allStrings) {w.println(text); }……写你的文件在循环中,因此你在每次循环迭代时都写字符串。

    您不需要 shortest 变量,只需使用布尔值来指示您读入的字符串是否比列表中已有的字符串短,如果是,则将其添加到你的清单。

    public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
        List<String> shortestStrings = new ArrayList<>();
    
        String line = r.readLine();
        shortestStrings.add(line);
    
        while(line != null) {
            boolean isShortert = Boolean.TRUE;
            for(String s: shortestStrings) {
                if(line.length() > s.length()) {
                    isShortert = Boolean.FALSE;
                }
            }
            if(isShortert) {
                shortestStrings.add(line);
            }
            line = r.readLine();
        }
    
        for (String text: shortestStrings) {
            w.println(text);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多