【问题】
public class Data { public static void main(String args[]) throws IOException { String gender; BufferedReader in = new BufferedReader(new FileReader("File Path")); String str; List<String> list = new ArrayList<String>(); while ((str = in.readLine()) != null) { list.add(str); } String[] stringArr = list.toArray(new String[0]); System.out.println(list.size()); Map<String, List<String>> genderNameList = new HashMap<String, List<String>>(); for (int i = 1; i < list.size(); i++) { String line = list.get(i); String[] values = line.split("\\|"); genderNameList.get(values[1]).add(values[1]); } }}
Facing problem with the code. I have to read a file Vicky.txt which contains the data like
1. 123456|Mr|Peter|..........|19||||| 2. 556667|Ms|amith|..........|26||||| 3. 098765|Mr|kevinpeter|..........|24||||| 4. 675584|Mr|kapaul|..........|26||||| 5. 234906|Ms|zim|..........|24||||| 6. 123456|Ms|tom|..........|24|||||
Where the age is in the 28Th location in each row of a file. Here I have to read the file and collect all Age values in array and I have to display how many people are there with the age as 26 and how many are there with the age 24 and 19.
Here is the code that I did but its not working....
【回答】
按照指定字段分组汇总,这是简单的结构化计算,但JAVA缺乏相应类库,代码会很难写。可用SPL实现,代码简单易懂:
A | |
1 | =file("d:\\source.txt").import(;,"|") |
2 | =A1.groups(#28;count(~)) |
A1:读取文本文件source.txt中的内容。
A2:按照第28个字段分组汇总。
上述代码很容易集成到JAVA,参考Java 如何调用 SPL 脚本