简体   繁体   中英

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

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....

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]:

System.out.println("This line retrieves a value: " + values[4] + "for the 5th column.");

And if you want the 28th char,

line.charAt(27);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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