从 CSV 创建对象数组
Creating an array of objects from a CSV
所以我在 CSV 文件上有一些文字,我已读入并通过打印出来确保它们已被准确读入,但我仍然不知道如何获取这些文字并将它们分配给每个人到数组中的特定对象。我的 for 循环中有 15 个的原因是因为我需要从 CSV 文件创建 15 个不同的 Card
对象。
问题:我如何创建一个包含多个 Card
已解析并读入的对象的数组?
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
int numberOfPlayers;
int playerNumber;
//reads in Cards.txt
File cards = new File("./src/Cards.txt");
Scanner fileScanner = new Scanner(cards);
for(int i = 0; i < 15; i++)
{
//while there is a new line in the data, goes to the next one
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(", ");
//while there is a new attribute to read in on a given line, reads data
while(lineScanner.hasNext())
{
String cardType = lineScanner.next();
String message = lineScanner.next();
Double amount = lineScanner.nextDouble();
//creates a Card
Card myCard = new Card(cardType, message, amount);
Card myCards[] = new Card[i]; //stuck here
你需要在循环外定义数组,然后在循环内添加。
就在循环之前你可以有
Card[] myCards = new Card[15];
然后在你的循环中你可以有
//creates a card
Card myCard = new Card(cardType, message, amount);
myCards[i] = myCard;
您还需要更改循环的结构。第一个 while 循环将读取文件中的每一行,第二个 while 循环将读取该行中的每张卡片,这实际上不是您想要的。您希望每个循环读取一张卡片。此代码应如下所示
Card[] myCards = new Card[15];
// only run this if the file has at least 1 line, and only run once
if (fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(", ");
for(int i = 0; i < 15; i++)
{
// read in one card
if (lineScanner.hasNext())
{
String cardType = lineScanner.next();
String message = lineScanner.next();
Double amount = lineScanner.nextDouble();
//creates a card
Card myCard = new Card(cardType, message, amount);
myCards[i] = myCard;
}
}
}
查看有关如何使用数组的 Oracle 数组文档以获取更多信息http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
如果情况发生变化并且您不知道要拥有多少张卡片,请改用 ArrayList
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
所以我在 CSV 文件上有一些文字,我已读入并通过打印出来确保它们已被准确读入,但我仍然不知道如何获取这些文字并将它们分配给每个人到数组中的特定对象。我的 for 循环中有 15 个的原因是因为我需要从 CSV 文件创建 15 个不同的 Card
对象。
问题:我如何创建一个包含多个 Card
已解析并读入的对象的数组?
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
int numberOfPlayers;
int playerNumber;
//reads in Cards.txt
File cards = new File("./src/Cards.txt");
Scanner fileScanner = new Scanner(cards);
for(int i = 0; i < 15; i++)
{
//while there is a new line in the data, goes to the next one
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(", ");
//while there is a new attribute to read in on a given line, reads data
while(lineScanner.hasNext())
{
String cardType = lineScanner.next();
String message = lineScanner.next();
Double amount = lineScanner.nextDouble();
//creates a Card
Card myCard = new Card(cardType, message, amount);
Card myCards[] = new Card[i]; //stuck here
你需要在循环外定义数组,然后在循环内添加。
就在循环之前你可以有
Card[] myCards = new Card[15];
然后在你的循环中你可以有
//creates a card
Card myCard = new Card(cardType, message, amount);
myCards[i] = myCard;
您还需要更改循环的结构。第一个 while 循环将读取文件中的每一行,第二个 while 循环将读取该行中的每张卡片,这实际上不是您想要的。您希望每个循环读取一张卡片。此代码应如下所示
Card[] myCards = new Card[15];
// only run this if the file has at least 1 line, and only run once
if (fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(", ");
for(int i = 0; i < 15; i++)
{
// read in one card
if (lineScanner.hasNext())
{
String cardType = lineScanner.next();
String message = lineScanner.next();
Double amount = lineScanner.nextDouble();
//creates a card
Card myCard = new Card(cardType, message, amount);
myCards[i] = myCard;
}
}
}
查看有关如何使用数组的 Oracle 数组文档以获取更多信息http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
如果情况发生变化并且您不知道要拥有多少张卡片,请改用 ArrayList http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html