Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串文本文件转换为对象数组_Java_Arrays_String_Text Files - Fatal编程技术网

Java 将字符串文本文件转换为对象数组

Java 将字符串文本文件转换为对象数组,java,arrays,string,text-files,Java,Arrays,String,Text Files,我想使用一个文本文件,将每一行放入一个类对象数组中。这是我的密码 try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("Patient.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

我想使用一个文本文件,将每一行放入一个类对象数组中。这是我的密码

try {
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("Patient.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    // Read file line by line
    while ((strLine = br.readLine()) != null) {
        // Print the content on the console
        System.out.println (strLine);
    }
    // Close the input stream
    in.close();
} catch (Exception e) { // Catch exception if any
    System.err.println("Error: " + e.getMessage());
}
我需要把它转换成一个数组对象,这就是我想要它的样子

Patient1 p[] = new Patient1[5];
p[0] = 001, "John", 17, 100, 65, 110, 110, 110, 109, 111, 114, 113, "Swaying, Nausea";
p[1] = 002, "Sun Min", 18, 101, 70, 113, 113, 110, 119, 111, 114, 113, "None";

等等。

您必须创建包含13个字段、构造函数和setter/getter的Patient类

public class Patient
{
   private String field1;
   private String field2; 
   private int field3;
   ....
   public void setField1(String field1) { this.field1=field1; }
   public String getField1() { return field1;}
   ...   
}
并使用
ArrayList
而不是数组

ArrayList<Patient> patients=new ArrayList<Patient>();
Patient pat=new Patient();
//set value to the patient object
patients.add(pat);
ArrayList患者=新建ArrayList();
患者pat=新患者();
//将值设置为患者对象
患者。添加(pat);

基于AVD的建议,您可以使用接受您的值的构造函数来完成您想要的任务-尽管不建议在构造函数中使用太多参数(为了可读性和调试)。根据数据的排序和读取方式,您甚至可以使用将所有内容转换为一种类型(即字符串)


您可以通过调用新患者来调用它(“John,“001”,“摇摆,恶心”…)。同样,这取决于你如何读取输入的数据;如果你不能以合理的方式将数据组合起来,那么你也可以选择创建。

我想要一匹小马。不过,说真的,你需要告诉我们你尝试了什么,然后问一个具体的问题。公众耐心1(整数i、字符串n、整数a、双w、整数h、整数bp1、整数bp2、整数bp3、整数bp4、整数bp5、整数bp6、整数bp7、字符串s){id=i;
name=n;
age=a;
weight=w;
height=h;
BP1=BP1;
BP2=BP2;
BP3=BP3;
BP4=BP4;
BP5=BP5;
BP6=BP6;
BP7=BP7;
SYM=s;
我现在该怎么办?我能用一个数组编号来识别每一行吗?@user1181810,你可以编辑你的问题,将你迄今为止尝试过的内容包括在内;这将更容易阅读。至于用数组编号来识别每一行,我恐怕不明白你的意思。如果你将每个
患者
对象存储到一个数组或
ArrayList
,您可以按照它们的输入顺序将它们取回。(如果需要,您可以扩展
toString()
并返回每个对象的值。)如果您仍然不确定,请更新您的问题以澄清您的意图。我有13个方法,如getID()和getName()。如何使用正确的方法连接字符串?
public class Patient {

   public Patient(String name, String id, String symptoms, String measurements) {  to get the individual fields from using a delimiter.

        // Stuff to fill in the fields goes here
    }
}