第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從一組數(shù)據(jù)中檢索最后一列

如何從一組數(shù)據(jù)中檢索最后一列

元芳怎么了 2022-06-15 16:25:10
我對java很陌生,我有一個(gè)關(guān)于它的問題。這些是我的代碼,我所做的是從一個(gè) txt 文件中過濾掉數(shù)據(jù)并將其保存到另一個(gè) txt 文件中。我現(xiàn)在要做的是檢索指定列(這是最后一列)并使用該數(shù)據(jù)進(jìn)行分析。我想在最后一列中搜索某個(gè)關(guān)鍵字出現(xiàn)的次數(shù)。每當(dāng)有匹配時(shí),計(jì)數(shù)器就會(huì)增加,文本用分隔符分隔。比如txt文件中有a,b,c,d,e,f,g,h,i,j。我想從 j 本身檢索一個(gè)指定的關(guān)鍵字,并有一個(gè)計(jì)數(shù)器,每次發(fā)生時(shí)都會(huì)增加。任何人都可以建議如何做到這一點(diǎn)?這些是我的代碼: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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

“當(dāng)您找到任何匹配項(xiàng)時(shí),您可以將結(jié)果放入數(shù)組/列表的字符串中。您可以返回列表/數(shù)組中的最后一個(gè)元素”;


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


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

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

如我所說


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


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


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


查看完整回答
反對 回復(fù) 2022-06-15
  • 2 回答
  • 0 關(guān)注
  • 104 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)