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