我需要帮助创建一个类,允许我读取一个文本文件,其中包含将存储到单个变量中的玩家数据,然后是数组。需要存储的变量是name,day,month,year,country,previousTeams []例如,文本文件中的一行将显示Cristiano Ronaldo,1986年,19,05,葡萄牙,曼联;皇马。
目前我最大的困难是试图了解如何将数组存储在文本文件中,如果有人能帮我解决这个问题,我将非常感激。
非常感谢大家的答案,我将如何使用拆分将数据提取到数组中?从这个例子来看,我试图将曼联和皇家马德里存放在阵列中使用&#34 ;;"用于指定要存储到数组中的下一个String的分隔符。这就是我想出来的,但还远未完成:
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[] = ?;
}
谢谢
答案 0 :(得分:0)
我最近从csv文件中提取了数据,并在读取后将内容放入类中。如下所示,您可以调整此值以适合txt文件。
private ArrayList<Solar> sols;
sols = new ArrayList();
BufferedReader crunchifyBuffer = null;
final String DELIMITER = ",";
try {
crunchifyBuffer = new BufferedReader(new FileReader("C:\\Users\\xxxx\\Documents\\Molar Forecasts\\fs-rad.20170421.21.csv"));
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
String line = null;
try {
line = crunchifyBuffer.readLine(); // Read first line
} catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
if (line != null) { try {
// Check it's not null
// line = crunchifyBuffer.readLine(); // Read second line
while ((line = crunchifyBuffer.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
Solar sol = new Solar(tokens[0], tokens[1], tokens[2], Double.parseDouble(tokens[3]));
sols.add(sol);
} } catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
}}
我的课程如下:
class Solar {
private String station;
private String period;
private String forecasttime;
private Double forecast;
public String details(){
return period;
}
public Double details1() {
return forecast;
}
public Solar(String station, String period, String forecasttime,Double forecast) {
this.station = station;
this.period = period;
this.forecasttime = forecasttime;
this.forecast = forecast;
}
public String getName() {
return station;
}
public void setName(String station) {
this.station = station;
}
public String getPrice() {
return period;
}
public void setPrice(String period) {
this.period = period;
}
public String getAuthor() {
return forecasttime;
}
public void setAuthor(String forecasttime) {
this.forecasttime = forecasttime;
}
public Double getAuthor1() {
return forecast;
}
public void setAuthor1(Double forecast) {
this.forecast = forecast;
}
}
答案 1 :(得分:0)
您可以使用split
从行中提取信息,这是一个示例,从文本到数组:
String str = "Cristiano Ronaldo, 19, 05, 1986, Portugal, Manchester United ; Real Madrid";
String[] spl = str.split("[,;]");
<强>输出强>
Cristiano Ronaldo,
19,
05,
1986,
Portugal,
Manchester United ,
Real Madrid
答案 2 :(得分:0)
我假设每个字段都有数据。 [即数据将以','分隔,之前的团队将以';'分隔&安培;单人游戏数据存储在单行中]
首先,您应该确定问题中的成员 我把“玩家”作为一个实体。
我们需要创建一个播放器对象的数组/列表来解决问题
这是输入文件包含的数据:
克里斯蒂亚诺罗纳尔多,1986年5月19日,葡萄牙,曼彻斯特联队;皇马Rahul Dravid,1986年5月19日,印度,RCB; RR; DD
ABD,19,05,1986,南非,DD; RCB
Dhoni,1986年5月19日,印度,CSK; RPS
R.Jadeja,1986年5月19日,印度,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) + "]";
}
}