从文本文件中读取文本数据到包含数组的对象中(Reading text data from a text file into an object containing an array)

编程入门 行业动态 更新时间:2024-10-14 04:29:28
从文本文件中读取文本数据到包含数组的对象中(Reading text data from a text file into an object containing an array)

我需要帮助创建一个类,它允许我读取一个文本文件,其中包含将存储在单个变量中的数据,然后是一个数组。 需要存储的变量是姓名,日期,月份,年份,国家,前一队[]。例如,文本文件中的一行将读取克里斯蒂亚诺罗纳尔多,19,05,1986,葡萄牙,曼联; 皇家马德里。

我目前最大的努力就是试图理解如何将数组存储在文本文件中,如果有人能够得到我,我将非常感激。

非常感谢大家的回答,我将如何使用拆分将数据提取到数组中? 从这个例子来看,我试图用“;”来存储曼联和皇马进入阵列。 分隔符来指定要存储到数组中的下一个字符串。 这就是我所想到的,但还远远没有完成:

while(input.hasNext()){ String name = input.next(); int day = input.nextInt(); int month = input.nextInt(); int year = input.nextInt(); String country = input.next(); String previousTeams[] = ?; }

谢谢

I need help creating a class that allows me to read in a text file that contains data on players that will be stored into single variables, and then an array. The variables that need to be store is name, day, month, year, country, previousTeams[] For example, one line in the text file will read Cristiano Ronaldo, 19, 05, 1986, Portugal, Manchester United ; Real Madrid.

My biggest struggle at the moment is trying to understand on how to store the array in the text file, if anyone could get me with this I would be very grateful.

Thank you very much for the answers everyone, how would I go about using the split to extract the data into the array? From the example, I am trying to store Manchester United and Real Madrid into an array using ";" delimiter to specify the next String to be stored into an array. This is what I have come up with, but is far from completed:

while(input.hasNext()){ String name = input.next(); int day = input.nextInt(); int month = input.nextInt(); int year = input.nextInt(); String country = input.next(); String previousTeams[] = ?; }

Thank you

最满意答案

我假设每个字段的数据都在那里。 [即数据将以','分隔,以前的球队将以';'分隔 &单人数据存储在单行中]

首先,您应该在我将“玩家”视为实体的问题中确定实体。

我们需要创建一个播放器对象的数组/列表来解决这个问题

这是输入文件包含的数据:

克里斯蒂亚诺罗纳尔多,19,05,1986,葡萄牙,曼联; 皇家马德里

Rahul Dravid,19,05,1986,印度,RCB; RR; DD

ABD,19,05,1986,南非,DD; RCB

Dhoni,19,05,1986,印度,CSK; RPS

R.Jadeja,19,05,1986,印度,CSK; GL

以下代码将完成我猜测的工作:

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Reader { public static void main(String[] a) { try{ String absoluteFilePath = "You Input File Absolute Path"; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(absoluteFilePath))); String singleEntry = null; Player player; // Iterate through the file content line by line while((singleEntry = bufferedReader.readLine())!=null) { // process each line and convert it into a object of player // that will be easier to handle going forward player = getPlayer(singleEntry); // Do anything you want to do with Player // I have just printed it out System.out.println(getPlayer(singleEntry)); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } } private static Player getPlayer(String input) { Player player = new Player(); // Tokenizer to break the line into tokens i.e information (here name, day etc) StringTokenizer stringTokenizer = new StringTokenizer(input); player.setName(stringTokenizer.nextToken(",").trim()); // trim() Just to remove spaces player.setDay(Integer.parseInt(stringTokenizer.nextToken(",").trim())); // trim() Just to remove spaces player.setMonth(Integer.parseInt(stringTokenizer.nextToken(",").trim())); // trim() Just to remove spaces player.setYear(Integer.parseInt(stringTokenizer.nextToken(",").trim())); // trim() Just to remove spaces player.setCountry(stringTokenizer.nextToken(",").trim()); // trim() Just to remove spaces player.setPreviousTeams(stringTokenizer.nextToken(",").trim().split("[;]")); // trim() Just to remove spaces return player; } } import java.util.Arrays; public class Player { private String name; private int day; private int month; private int year; private String country; private String[] previousTeams; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String[] getPreviousTeams() { return previousTeams; } public void setPreviousTeams(String[] previousTeams) { this.previousTeams = previousTeams; } @Override public String toString() { return "Player [name=" + name + ", day=" + day + ", month=" + month + ", year=" + year + ", country=" + country + ", previousTeams=" + Arrays.toString(previousTeams) + "]"; } }

I am assuming that data will be there for each field. [i.e data will be separated with ',' and previous teams will be separated with ';' & Single player data is stored in single lines]

First of all you should identify entites in the Problem I have taken 'Player' as an Entity.

We need to create an Array/List of Player Objects to solve the Problem

This is the data contained by Input file:

Cristiano Ronaldo, 19, 05, 1986, Portugal, Manchester United ; Real Madrid

Rahul Dravid, 19, 05, 1986, India, RCB ; RR ; DD

ABD, 19, 05, 1986, South Africa, DD; RCB

Dhoni, 19, 05, 1986, India, CSK; RPS

R.Jadeja, 19, 05, 1986, India, CSK; GL

Following Code will do the Job I guess :

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Reader { public static void main(String[] a) { try{ String absoluteFilePath = "You Input File Absolute Path"; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(absoluteFilePath))); String singleEntry = null; Player player; // Iterate through the file content line by line while((singleEntry = bufferedReader.readLine())!=null) { // process each line and convert it into a object of player // that will be easier to handle going forward player = getPlayer(singleEntry); // Do anything you want to do with Player // I have just printed it out System.out.println(getPlayer(singleEntry)); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } } private static Player getPlayer(String input) { Player player = new Player(); // Tokenizer to break the line into tokens i.e information (here name, day etc) StringTokenizer stringTokenizer = new StringTokenizer(input); player.setName(stringTokenizer.nextToken(",").trim()); // trim() Just to remove spaces player.setDay(Integer.parseInt(stringTokenizer.nextToken(",").trim())); // trim() Just to remove spaces player.setMonth(Integer.parseInt(stringTokenizer.nextToken(",").trim())); // trim() Just to remove spaces player.setYear(Integer.parseInt(stringTokenizer.nextToken(",").trim())); // trim() Just to remove spaces player.setCountry(stringTokenizer.nextToken(",").trim()); // trim() Just to remove spaces player.setPreviousTeams(stringTokenizer.nextToken(",").trim().split("[;]")); // trim() Just to remove spaces return player; } } import java.util.Arrays; public class Player { private String name; private int day; private int month; private int year; private String country; private String[] previousTeams; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String[] getPreviousTeams() { return previousTeams; } public void setPreviousTeams(String[] previousTeams) { this.previousTeams = previousTeams; } @Override public String toString() { return "Player [name=" + name + ", day=" + day + ", month=" + month + ", year=" + year + ", country=" + country + ", previousTeams=" + Arrays.toString(previousTeams) + "]"; } }

更多推荐

本文发布于:2023-07-16 12:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   文本文件   象中   文本   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!