简体   繁体   English

解析文本文件时出现问题

[英]Problems parsing text file

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我必须读取一个包含数据的文件 Vicky.txt

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.年龄位于文件每行的第 28 位。 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.在这里,我必须读取文件并收集数组中的所有年龄值,我必须显示年龄为 26 的人数以及年龄为 24 和 19 的人数。

Here is the code that I did but its not working....这是我所做的代码,但它不起作用....

Assuming your split works:假设您的拆分有效:

 String[] values = line.split("\\|");

What does this line do?这条线有什么作用?

genderNameList.get(values[1]).add(values[1]);

It would seem like you need to process values[4]:您似乎需要处理值[4]:

System.out.println("This line retrieves a value: " + values[4] + "for the 5th column."); System.out.println("这一行检索了一个值:" + values[4] + "for the 5th column.");

And if you want the 28th char,如果你想要第 28 个字符,

line.charAt(27);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM