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

java-读取输入行并输出重复项,java,text,line,Java,Text,Line,所以我正在学习如何读取文本文件之类的东西,我试图制作一个程序,一次读取一行输入,当且仅当当前行小于目前读取的任何其他行时,才输出当前行。根据String.compareTo()的定义,相对于字符串的常规顺序较小 当我尝试运行代码时,会出现错误“列表无法解析为类型”和“ArrayList无法解析为类型”。我很困惑为什么我的程序中会出现这个错误,我想知道是否还有其他我应该使用的东西 package comp; import java.io.BufferedReader; import java.i

所以我正在学习如何读取文本文件之类的东西,我试图制作一个程序,一次读取一行输入,当且仅当当前行小于目前读取的任何其他行时,才输出当前行。根据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);
        }
    }
}
package comp;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.util.HashSet;
公共课第2部分{
公共静态void doIt(BufferedReader r,PrintWriter w)引发IOException{
List allStrings=new ArrayList();
弦线;
String shortest=allStrings.get(0);
而((line=r.readLine())!=null){
用于(字符串s:所有字符串){
如果(s.length()>shortest.length()){
最短=s;
直线=最短;
}
}
allStrings.add(行);
for(字符串文本:所有字符串){
w、 println(文本);
}
}
}
公共静态void main(字符串[]args){
试一试{
缓冲读取器r;
版画作家w;
如果(args.length==0){
r=新的BufferedReader(新的InputStreamReader(System.in));
w=新的PrintWriter(系统输出);
}else if(args.length==1){
r=新的BufferedReader(新的文件读取器(args[0]);
w=新的PrintWriter(系统输出);
}否则{
r=新的BufferedReader(新的文件读取器(args[0]);
w=新的PrintWriter(新的FileWriter(args[1]);
}
长启动=System.nanoTime();
doIt(r,w);
w、 冲洗();
长时间停止=System.nanoTime();
System.out.println(“执行时间:+10e-9*(停止-启动));
}捕获(IOE异常){
系统错误println(e);
系统退出(-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
444
您的代码还存在以下错误:

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

 // It throws IndexOutOfBoundsException!!!
    String shortest = allStrings.get(0);
List allStrings=new ArrayList();
//它抛出IndexOutOfBoundsException!!!
String shortest=allStrings.get(0);

这里有几个问题。如果您运行代码,您将在
doIt()
中获得
IndexOutOfBoundException
,因为您的
allStrings
列表为空,因此当您调用
String shortest=allStrings.get(0)调用列表中没有的内容

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);
    }
}
另外,if(s.length()>shortest.length())
的逻辑有点奇怪,因为实际上您只比较列表中的字符串,而不是与从文件中读取的字符串进行比较。因此,您永远无法确定从文件中读取的行是否比您已有的任何行都小

最后,这个代码<代码>用于(字符串文本:所有字符串){w.println(文本);}。。。写文件是在循环中的,因此每次循环迭代都要写字符串

您不需要最短的变量,只需使用布尔值指示您读入的字符串是否比列表中已有的字符串短,并且仅当它短时,才将其添加到列表中

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);
    }
}
publicstaticvoiddoit(BufferedReader r,PrintWriter w)抛出IOException{
List shortestStrings=new ArrayList();
字符串行=r.readLine();
最短字符串。添加(行);
while(行!=null){
布尔值isShortert=boolean.TRUE;
用于(字符串s:最短字符串){
如果(line.length()>s.length()){
isShortert=Boolean.FALSE;
}
}
如果(isShortert){
最短字符串。添加(行);
}
line=r.readLine();
}
for(字符串文本:最短字符串){
w、 println(文本);
}
}

你能把你的全班都发出去吗?我在doIt中看到了bug,但我想在证明答案之前调试代码。