如何从一组数据中检索最后一列

我对java很陌生,我有一个关于它的问题。


这些是我的代码,我所做的是从一个 txt 文件中过滤掉数据并将其保存到另一个 txt 文件中。


我现在要做的是检索指定列(这是最后一列)并使用该数据进行分析。


我想在最后一列中搜索某个关键字出现的次数。


每当有匹配时,计数器就会增加,文本用分隔符分隔。


比如txt文件中有a,b,c,d,e,f,g,h,i,j。我想从 j 本身检索一个指定的关键字,并有一个计数器,每次发生时都会增加。


任何人都可以建议如何做到这一点?


这些是我的代码:


import java.io.*;


public class java {


    public static void main(String[] args) {

        String searchWord = "asiana";


        try (BufferedReader in = new BufferedReader(

                new FileReader("D:\\Downloads\\dataAnalysis1.txt"));

            BufferedWriter out = new BufferedWriter(

                new FileWriter("D:\\Downloads\\output.txt"))) {


            String line;


            while ((line = in.readLine()) != null) {

                System.out.println(line);


                // extract by lines and write to file

                if (line.contains(searchWord)) {

                    out.write(line);

                    out.newLine();

                }

            }

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

}


元芳怎么了
浏览 129回答 2
2回答

肥皂起泡泡

“当您找到任何匹配项时,您可以将结果放入数组/列表的字符串中。您可以返回列表/数组中的最后一个元素”;String searchWord = "asiana";&nbsp;List<String> list = new LinkedList<String>();&nbsp; &nbsp; try (BufferedReader in = new BufferedReader(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new FileReader("D:\\Downloads\\dataAnalysis1.txt"));&nbsp; &nbsp; &nbsp; &nbsp; BufferedWriter out = new BufferedWriter(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new FileWriter("D:\\Downloads\\output.txt"))) {&nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; while ((line = in.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // extract by lines and write to file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.contains(searchWord)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.addLast(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.newLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; //print the last element from the list.&nbsp; &nbsp; print: list.get(list.size()-1);

慕妹3146593

如我所说如果它是 CSV,那么你有一些分隔符(, or ;),当你阅读文件时,你已经在 line 变量中获取了整行,你可以使用 String.split 然后参考新数组上的 .length ,你将获得,那应该给你最后一列的值但请注意,正如 M. Prokhorov 所提到的,如果数据也包含转义分隔符,那么它将无法正常工作,这取决于数据public static String getLastColumnValue(String value, char separator) {&nbsp; &nbsp; &nbsp; &nbsp; //https://stackoverflow.com/questions/54630596/how-to-retrieve-last-column-from-set-of-data&nbsp; &nbsp; &nbsp; &nbsp; String[] tokens = value.split(String.valueOf(separator));&nbsp; &nbsp; &nbsp; &nbsp; //indexed from zero -> length -1&nbsp; &nbsp; &nbsp; &nbsp; if(tokens.length>0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return tokens[tokens.length-1];&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }有测试System.out.println(StringUtils.getLastColumnValue("first col;second;foo;fooolastsemicol", ';'));//fooolastsemicolSystem.out.println(StringUtils.getLastColumnValue("first col,second,foo,fooolastcomma", ','));//fooolastcomma
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java