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

时间:2016-01-24 21:37:42

标签: java arrays object

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

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

}

}

1 个答案:

答案 0 :(得分:1)

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

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

}

}