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

Java 如何读取文本文件并使用它填充对象数组的数据?,java,arrays,object,Java,Arrays,Object,我正在尝试创建一个相当大的程序。我需要程序做的一件事是读取一个文本文件并使用它填充数组中多个对象的值。在我即将发布的程序中,数组的每个对象都有三种不同的数据类型,我需要通过让程序读取文本文件并从每行中提取正确的数据片段来填充值。我已经知道如何让我的程序读取文本文件并逐行打印数据,但我不知道如何创建一个循环,从文本文件的一行中选择特定的数据片段,并将它们放置在每个对象的适当位置。下面的程序是一个更大的程序的简化版本,该程序已接近完成。在这件事上的任何帮助都将不胜感激 package cloneCo

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

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);
}

}

}
包克隆计数器;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类克隆计数器{
公共字符串名称;
公共信息;
公共双权;
cloneCounter(字符串cloneName,int timeSpentLiving,双重)
{
名称=克隆名称;
年龄=生活时间;
重量=重量;
}
公共静态void main(字符串[]args){
克隆计数器[]克隆=新克隆计数器[3];
//这就是文本文件的外观。
//比利22188.25
//山姆46301.77
//约翰851.22
//可以读取和打印文本文件的代码
字符串fileName=“data.txt”;
扫描仪输入流=空;
System.out.println(“文件“+fileName+”\n包含以下行:\n”);
尝试
{
inputStream=new Scanner(新文件(“data.txt”);//正确读取了txt文件。
}
catch(filenotfounde异常)
{
System.out.println(“打开文件时出错”+文件名);
系统出口(0);
}
列表克隆=新建ArrayList();
while(inputStream.hasNextLine()){
String line=inputStream.nextLine();
String[]data=line.split(“”);
cloneCounter clone=newclonecounter(数据[0],Integer.parseInt(数据[1]),Double.parseDouble(数据[2]);
克隆。添加(克隆);
}
}
inputStream.close();
//临时占位符,用于填充对象的值,直到我知道如何从文本文件导入和实现数据为止
克隆[0]=新克隆计数器(“比利”,22188.25);
克隆[1]=新克隆计数器(“Sam”,46301.77);
克隆[2]=新克隆计数器(“约翰”,8,51.22);

对于(int i=0;i如果数据由单个空格分隔,则可以执行以下操作:

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);
           }
List clones=new ArrayList();
while(inputStream.hasNextLine()){
String line=inputStream.nextLine();
String[]data=line.split(“”);
cloneCounter clone=newclonecounter(数据[0],Integer.parseInt(数据[1]),Double.parseDouble(数据[2]);
克隆。添加(克隆);
}
编辑:这是完整的程序,只需复制并粘贴它,它就可以正常工作(已测试):

包克隆计数器;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公共类克隆计数器{
公共字符串名称;
公共信息;
公共双权;
cloneCounter(字符串cloneName,int timeSpentLiving,双重)
{
名称=克隆名称;
年龄=生活时间;
重量=重量;
}
公共静态void main(字符串[]args){
//这就是文本文件的外观。
//比利22188.25
//山姆46301.77
//约翰851.22
//可以读取和打印文本文件的代码
字符串fileName=“data.txt”;
扫描仪输入流=空;
System.out.println(“文件“+fileName+”\n包含以下行:\n”);
尝试
{
inputStream=new Scanner(新文件(“data.txt”);//正确读取了txt文件。
}
catch(filenotfounde异常)
{
System.out.println(“打开文件时出错”+文件名);
系统出口(0);
}
列表克隆=新建ArrayList();
while(inputStream.hasNextLine()){
String line=inputStream.nextLine();
String[]data=line.split(“”);
cloneCounter clone=newclonecounter(数据[0],Integer.parseInt(数据[1]),Double.parseDouble(数据[2]);
克隆。添加(克隆);
}
inputStream.close();

对于(int i=0;i“Yourdata”是什么意思?Yourdata是要使用的对象的名称:Yourdata=new Clonecounter()@Tom检查编辑请我将您的所有对象放入ArrayList谢谢。我会尝试一下。我尝试使用您告诉我的方法使程序运行。我对do while循环进行了一些更改,希望可以使用变量“x”但是,为了帮助我填写每个克隆的数据,我在cloneCounter.cloneCounter.main(cloneCounter.java:51)的线程“main”java.lang.NullPointerException中遇到了错误消息Exception
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);
}

}

}