简体   繁体   中英

Reading specific data from a line in a text file to respective arrays, in Java

在此处输入图像描述

This is in a text file

After each comma on the line, it's a different piece of data, eg

the first data is first name, then surname, then contact number, then address, then college, then college id, then course, then specialization

I need to write a java program that will read the text file and for each line, and for each piece of data, will store the information into respective arrays in the program, for example, all the “first names” read will be stored in an array called “first name” and so on.

This was easy to for me to write in python using line.split(“,”) but java is totally different for me to understand

Yes you can also achieve this in java using split method by splitting the each line by separate in your case it is comma and you will get string array.Then iterate over the array and map your keys.

Consider below example

String line = "Amit, 29"; String []values = line.split(","); String name = values[0]; String age = values[1];

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