有人可以帮我把这些数据输入到ArrayList中的3个对象中(每个玩家一个)吗?
文本文件示例:
Steve| Barkley| 258| 300
Carl |Johnson |142
Frank|Davidson
Java代码:
//couldn't write the normal jfilechoose code above due to space
File playerFile = new File(selectedFile.getAbsolutePath());
Scanner in = new Scanner(playerFile);
String[] playerData; //array to hold data
while (in.hasNext()) {
String data = in.nextLine();
playertData = data.split("\\|");
playerData = Arrays.copyOf(playerData,playerData.length+1);
String firstName = playerData[0];
String lastName = playerData[1];
double playererayear1 = Double.parseDouble(playerData[2]==null?"0":playerData[2]);
double playererayear2 = Double.parseDouble(playerData[3]==null?"0":playerData[3]);
double playererayear3 = Double.parseDouble(playerData[4] == null?"0":playerData[4]);
答案 0 :(得分:0)
您可以创建一个单独的类来保存有关播放器的信息。
public class Player {
private String firstName;
private String lastName;
private double playerEraYear1;
private double playerEraYear2;
private double playerEraYear3;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getPlayerEraYear1() {
return playerEraYear1;
}
public void setPlayerEraYear1(double playerEraYear1) {
this.playerEraYear1 = playerEraYear1;
}
public double getPlayerEraYear2() {
return playerEraYear2;
}
public void setPlayerEraYear2(double playerEraYear2) {
this.playerEraYear2 = playerEraYear2;
}
public double getPlayerEraYear3() {
return playerEraYear3;
}
public void setPlayerEraYear3(double playerEraYear3) {
this.playerEraYear3 = playerEraYear3;
}
}
现在你可以解析文件,为每个玩家创建一个Player
对象并将它们添加到列表中
public void parseFile() {
File playerFile = new File(selectedFile.getAbsolutePath());
Scanner in = new Scanner(playerFile);
List<Player> players = new ArrayList<>();
while (in.hasNext()) {
String data = in.nextLine();
String[] playerData = data.split("\\|");
Player p = new Player();
p.setFirstName(playerData[0]);
p.setLastName(playerData[1]);
if (playerData.size >= 3) {
double playererayear1 = Double.parseDouble(playerData[2] == null ? "0" : playerData[2]);
p.setPlayerEraYear1(playererayear1);
}
if (playerData.size >= 4) {
double playererayear2 = Double.parseDouble(playerData[3] == null ? "0" : playerData[3]);
p.setPlayerEraYear2(playererayear2);
}
if (playerData.size >= 5) {
double playererayear3 = Double.parseDouble(playerData[4] == null ? "0" : playerData[4]);
p.setPlayerEraYear3(playererayear3);
}
players.add(p);
}
}
答案 1 :(得分:0)
package com.aegle.validator;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
List<Player> playerList = new ArrayList<Player>();
File playerFile = new File("");//Set your file path
Scanner in = new Scanner(playerFile);
while (in.hasNext()) {
String[] data = in.nextLine().split("\\|");
Player player = new Player(data[0], data[1]);
player.setYears(Arrays.copyOfRange(data, 2, data.length));
playerList.add(player);
}
System.out.println(playerList);//Just to test
}
}
class Player {
String firstName;
String lastName;
String[] years;
public Player(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setYears(String[] years) {
this.years = years;
}
//Introduce getters as you need
@Override
public String toString() {
return "Player{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", years=" + Arrays.toString(years) +
'}';
}
}