亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從一組數據中檢索最后一列

如何從一組數據中檢索最后一列

元芳怎么了 2022-06-15 16:25:10
我對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();        }    }}
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

“當您找到任何匹配項時,您可以將結果放入數組/列表的字符串中。您可以返回列表/數組中的最后一個元素”;


String searchWord = "asiana";

 List<String> list = new LinkedList<String>();


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

                list.addLast(line);

                out.newLine();

            }

        }

    } catch (IOException ex) {

        ex.printStackTrace();

    }

    //print the last element from the list.

    print: list.get(list.size()-1);


查看完整回答
反對 回復 2022-06-15
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

如我所說


如果它是 CSV,那么你有一些分隔符(, or ;),當你閱讀文件時,你已經在 line 變量中獲取了整行,你可以使用 String.split 然后參考新數組上的 .length ,你將獲得,那應該給你最后一列的值


但請注意,正如 M. Prokhorov 所提到的,如果數據也包含轉義分隔符,那么它將無法正常工作,這取決于數據


public static String getLastColumnValue(String value, char separator) {

        //https://stackoverflow.com/questions/54630596/how-to-retrieve-last-column-from-set-of-data

        String[] tokens = value.split(String.valueOf(separator));

        //indexed from zero -> length -1

        if(tokens.length>0) {

            return tokens[tokens.length-1];

        }else {

            return null;

        }

    }

有測試


System.out.println(StringUtils.getLastColumnValue("first col;second;foo;fooolastsemicol", ';'));

//fooolastsemicol

System.out.println(StringUtils.getLastColumnValue("first col,second,foo,fooolastcomma", ','));

//fooolastcomma


查看完整回答
反對 回復 2022-06-15
  • 2 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號