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

huangapple 未分类评论45阅读模式
英文:

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

问题

这在一个文本文件中

每行的逗号后面是不同的数据片段,例如:

第一个数据是名字,然后是姓氏,然后是联系电话,然后是地址,然后是大学,然后是大学ID,然后是课程,最后是专业

我需要编写一个Java程序,读取文本文件,对于每一行和每个数据片段,将信息存储到程序中的相应数组中,例如,所有读取的“名字”将被存储在一个名为“名字”的数组中,依此类推。

在Python中,我可以很容易地使用`line.split(",")`来实现这一点,但是对我来说,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, e.g.

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

答案1

得分: 0

是的,您也可以在Java中使用split方法来实现这一点,通过将每一行按照逗号(在您的情况下)分割,您将获得一个字符串数组。然后遍历该数组并映射您的键。

考虑以下示例:

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

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

huangapple
  • 本文由 发表于 2020年4月3日 21:13:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/61012710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定