gpt4 book ai didi

java - 如何读取文本文件并使用它来填充对象数组的数据?

转载 作者:行者123 更新时间:2023-11-30 07:38:50 25 4
gpt4 key购买 nike

我正在尝试创建一个相当大的程序。我需要程序做的事情之一是读取文本文件并使用它来填充数组的多个对象的值。在我即将发布的程序中,数组的每个对象都具有三种不同的数据类型,我需要通过让我的程序读取文本文件并从每个对象中提取正确的数据来填充这些值。线。我已经知道如何让我的程序读取文本文件并逐行打印出数据,但我不知道如何创建一个循环,从文本文件中的一行中挑选出特定的数据片段并将它们放入在每个对象的适当位置。以下程序是一个已接近完成的较大程序的简化版本。任何有关此事的帮助将不胜感激。

package cloneCounter;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class cloneCounter {
public String name;
public int age;
public double weight;

cloneCounter(String cloneName, int timeSpentLiving, double heaviness)
{
name = cloneName;
age = timeSpentLiving;
weight = heaviness;

}

public static void main(String[] args) {


cloneCounter [] clone = new cloneCounter[3];


//This is what the textfile looks like.
//Billy 22 188.25
//Sam 46 301.77
//John 8 51.22

//code that can read and print the textfile
String fileName = "data.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName + "\ncontains the following lines:\n");
try
{
inputStream = new Scanner(new File("data.txt"));//The txt file is being read correctly.
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}


List<cloneCounter> clones = new ArrayList<cloneCounter>();
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
String[] data = line.split(" ");
cloneCounter clone = new cloneCounter(data[0],Integer.parseInt(data[1]),Double.parseDouble(data[2]));
clones.add(clone);
}
}


inputStream.close();





//temporary placeholders to fill in the values for the objects until i can figure out how to import and implement the data from a text file
clone[0] = new cloneCounter ("Billy", 22, 188.25);
clone[1] = new cloneCounter ("Sam", 46, 301.77);
clone[2] = new cloneCounter ("John", 8, 51.22);



for(int i=0; i<3; i++)
{
System.out.println(clone[i].name + " " + clone[i].age + " " + clone[i].weight);
}

}

}

最佳答案

如果您的数据由单个空格分隔,那么您可以执行以下操作:

List<cloneCounter> clones = new ArrayList<cloneCounter>();     
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
String[] data = line.split(" ");
cloneCounter clone = new cloneCounter(data[0],Integer.parseInt(data[1]),Double.parseDouble(data[2]));
clones.add(clone);
}

编辑:这是完整的程序,只需复制并粘贴它即可正常工作(经过测试):

package cloneCounter;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class cloneCounter {
public String name;
public int age;
public double weight;

cloneCounter(String cloneName, int timeSpentLiving, double heaviness)
{
name = cloneName;
age = timeSpentLiving;
weight = heaviness;

}

public static void main(String[] args) {



//This is what the textfile looks like.
//Billy 22 188.25
//Sam 46 301.77
//John 8 51.22

//code that can read and print the textfile
String fileName = "data.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName + "\ncontains the following lines:\n");
try
{
inputStream = new Scanner(new File("data.txt"));//The txt file is being read correctly.
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}


List<cloneCounter> clones = new ArrayList<cloneCounter>();
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
String[] data = line.split(" ");
cloneCounter clone = new cloneCounter(data[0],Integer.parseInt(data[1]),Double.parseDouble(data[2]));
clones.add(clone);
}

inputStream.close();






for(int i=0; i<3; i++)
{
System.out.println(clones.get(i).name + " " + clones.get(i).age + " " + clones.get(i).weight);
}

}

}

关于java - 如何读取文本文件并使用它来填充对象数组的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com