简体   繁体   English

从文本文件中的一行读取特定数据到相应的 arrays,在 Java

[英]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第一个数据是名字,然后是姓氏,然后是联系电话,然后是地址,然后是大学,然后是大学 ID,然后是课程,然后是专业

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.我需要编写一个 java 程序,它将读取文本文件,并且对于每一行以及每条数据,将信息存储到程序中相应的 arrays 中,例如,读取的所有“名字”都将存储在一个名为“名字”的数组等等。

This was easy to for me to write in python using line.split(“,”) but java is totally different for me to understand这对我来说很容易使用line.split(“,”)在 python 中编写,但是 java 对我来说完全不同

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.是的,您也可以在 java 中使用拆分方法实现此目的,在您的情况下,它是逗号,将每一行分开拆分,您将获得字符串数组。然后遍历数组和 map 您的密钥。

Consider below example考虑下面的例子

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

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

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