java 板


LINE

我有一筆通訊資料裡面記載 name, phone, email 只有 name 是必需的 phone 跟 email 可有可無 文字檔內容如下: name pc mxn phone 3246436 email [email protected] name fire ox email [email protected] name kk mxn phone 2135455 email [email protected] 我想對這個文字檔作排序 e.g. 依 name 排序、或是依 phone 排序 目前只作了依 name 的排序 不過輸出的資料只有第一筆 應該是讀進來的資料在第一個迴圈就用掉了 所以第二、第三沒有資料可讀 不知道該怎麼寫才行 另外,如果想對 phone 或是 email 排序,該怎麼作呢 跟 name 的排序好像不太一樣 程式碼如下: import java.util.*; import java.io.*; import java.lang.*; public class Main { public static void main(String[] args) throws Exception{ String inputfile = "input.txt"; String outputfile = "output.txt"; String[] person = new String[3]; FileReader input1 = new FileReader(inputfile); Scanner content1 = new Scanner(input1); String str; int count = 0; // 找出資料內的全部姓名並且存在 person 裡面 while (content1.hasNext()){ str = content1.next(); if (str.equals("name")){ person[count] = content1.next()+" "+content1.next(); count += 1; } } // 將 person 排序 Arrays.sort(person); String newLine = System.getProperties().getProperty("line.separator"); FileWriter output = new FileWriter(outputfile,true); BufferedReader input = new BufferedReader(new FileReader(inputfile)); String line; // 因為有三筆資料,所以將資料讀取三次 for (int i=0;i<3;i++){ input = new BufferedReader(new FileReader(inputfile)); Scanner content = new Scanner(input); while (content.hasNextLine()){ line = content.nextLine(); if (line.indexOf(person[i])>=0){ // 如果該行有這個 name 就將該行寫入 output 直到讀到空白行 while (!line.trim().equals("")){ output.write(line + newLine); line = content.nextLine(); } } } } input.close(); output.close(); } } --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.225.166.232
1F:推 iensue:我有寫過類似的, 我是把他存成一個Object, 05/19 17:09
2F:→ iensue:Person(String name, String phone, String e-mail) 05/19 17:11
3F:→ amike:那要怎麼做 object 根據某項資料的排序呢? 05/19 17:11
4F:推 iensue:你在線上哦...?我的msn: [email protected] 05/19 17:14
5F:推 keter:先存成object 再implements Comparable~ 這是關鍵步驟 05/19 19:36
6F:推 ClareQ:欲抽換排序方法可用Comparator 05/19 20:10
7F:→ amike:現在排序大概是沒問題了,不過在處理文字檔時我是用暴力法 05/19 20:34
8F:→ amike:把 name、phone、email抽出來丟給 object,有比較好的方法嗎 05/19 20:37
9F:推 jayfish:你的文字檔可以考慮用csv的檔案格式 05/22 17:02







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:iOS站內搜尋

TOP