Java将值从数组传递到新对象实例

Java将值从数组传递到新对象实例,java,arrays,file,object,Java,Arrays,File,Object,我在向构造函数传递数组值时遇到一些问题。我有一个文件.txt,其中包含一些带有值的行,例如: 彼得|柏林|德国| 0930295235 |福 我想出了如何将线转换成数组。但我不知道如何将数组中的值传递给构造函数,以便将数组转换为具有数组属性的对象 以下是主要课程: import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputSt

我在向构造函数传递数组值时遇到一些问题。我有一个
文件.txt
,其中包含一些带有值的行,例如:

彼得|柏林|德国| 0930295235 |福

我想出了如何将线转换成数组。但我不知道如何将数组中的值传递给构造函数,以便将数组转换为具有数组属性的对象

以下是主要课程:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;


public class FileReader {

    public static void main(String[] args) {
        try {
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("file.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            // Read the file line by line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                String s[] = strLine.split("\\|");
                // System.out.println(java.util.Arrays.toString(s));
                // System.out.println (strLine);
                for (String element : s) {
                    System.out.println(element);
                    // HERE I NEED TO PASS THE ARRAY VALUES FOR EACH LINE TO TRANSFORM THE ARRAYS TO OBJECTS
                    Item item = new Item(element,element,element,element,element);
                    System.out.println("*************************************");
                }
                // Close the input stream
                in.close();
            } catch (Exception e) { //Catch exception if any
                System.err.println("Error: " + e.getMessage());
        }
    }
}
以下是课堂项目:

public class Item {

    private String name;
    private String city;
    private String state;
    private String number;
    private String foo;

    public Object(String name, String city, String state, String number,String foo){
        this.name = name;
        this.city = city;
        this.state = state;
        this.number = number;
        this.foo = foo; 
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getFoo() {
        return foo;
    }
    public void setFoo(String foo) {
        this.foo = foo;
    }
}

我需要对象来处理更多的数据。非常感谢您的帮助。

我想您不需要这个
循环

for(String element : s)
{
}
如果您希望从数组中提取特定的
字符串
s,则应使用以下命令:

String name = s[0];

但是要小心不要选择数组范围之外的项。

我认为循环不需要这个

for(String element : s)
{
}
如果您希望从数组中提取特定的
字符串
s,则应使用以下命令:

String name = s[0];

但请注意不要选择数组范围之外的项。

将for循环更改为:

for(int i=0;i<s.size();i+=5) {
      System.out.println(s[i]);
      Object item = new Object(s[i],s[i+1],s[i+2],s[i+3],s[i+4]);
 }

for(int i=0;i将for循环更改为:

for(int i=0;i<s.size();i+=5) {
      System.out.println(s[i]);
      Object item = new Object(s[i],s[i+1],s[i+2],s[i+3],s[i+4]);
 }

for(inti=0;i我认为您想要做的事情如下所示,尽管还不完全清楚

String s[] = strLine.split("\\|");
Item item = new Item(s[0], s[1], s[2], s[3], s[4]);
还请注意,在创建新的
对象后,它几乎会立即超出范围,因此需要将其存储在
while
循环之外声明的某种容器中:

List<Item> items = new ArrayList<Item>();
String line;

while((line = br.readLine()) != null) {
    String[] words = line.split("\\|");
    Item item = new Item(words[0], words[1], words[2], words[3], words[4]);
    items.add(item);
}
List items=new ArrayList();
弦线;
而((line=br.readLine())!=null){
String[]words=line.split(“\\\\”);
项目=新项目(词语[0]、词语[1]、词语[2]、词语[3]、词语[4]);
项目。添加(项目);
}

我认为您想做的事情如下,尽管还不完全清楚

String s[] = strLine.split("\\|");
Item item = new Item(s[0], s[1], s[2], s[3], s[4]);
还请注意,在创建新的
对象后,它几乎会立即超出范围,因此需要将其存储在
while
循环之外声明的某种容器中:

List<Item> items = new ArrayList<Item>();
String line;

while((line = br.readLine()) != null) {
    String[] words = line.split("\\|");
    Item item = new Item(words[0], words[1], words[2], words[3], words[4]);
    items.add(item);
}
List items=new ArrayList();
弦线;
而((line=br.readLine())!=null){
String[]words=line.split(“\\\\”);
项目=新项目(词语[0]、词语[1]、词语[2]、词语[3]、词语[4]);
项目。添加(项目);
}

这里不应该有for循环,只需将每个数组值传递给
对象的构造函数,首先检查值的数量是否正确。新的
对象已经在Java中使用,因此调用类(记住对象和类之间的区别),
MyObj

MyObj myObj = null; // De
String s[] = strLine.split("\\|");
if (s.length==5){
  myObj = new MyObj(s[0],s[1],s[2],s[3],s[4]);
} else {
  System.err.println("Wrong number of arguments on the line"+strLine);
}
很明显,可以使用不带参数的构造函数,然后使用

myObj = new MyObj();
myObj.setName( s[0]); //etc

它的可读性稍高一些,至于哪些数据进入哪个字段

这里不应该有for循环,只要将每个数组值传递给
对象
的构造函数,首先检查您是否有正确数量的值。新的
对象
已经在Java中使用,所以调用您的类(记住对象和类之间的区别),
MyObj

MyObj myObj = null; // De
String s[] = strLine.split("\\|");
if (s.length==5){
  myObj = new MyObj(s[0],s[1],s[2],s[3],s[4]);
} else {
  System.err.println("Wrong number of arguments on the line"+strLine);
}
很明显,可以使用不带参数的构造函数,然后使用

myObj = new MyObj();
myObj.setName( s[0]); //etc

由于您知道
file.txt的结构以及
字符串[]s的长度,所以可以调用数组中的每个元素:

for (String element : s) {
    System.out.println(element);
    Item item = new Item(s[0], s[1], s[2], s[3], s[4]);
}
为了保存
项项,并且不使用循环中的下一个元素覆盖它们,可以将其与项一起保存到数组中(从声明数组开始):


由于您知道
file.txt的结构,因此知道
String[]s
的长度,因此可以调用数组中的每个元素:

for (String element : s) {
    System.out.println(element);
    Item item = new Item(s[0], s[1], s[2], s[3], s[4]);
}
为了保存
项项,并且不使用循环中的下一个元素覆盖它们,可以将其与项一起保存到数组中(从声明数组开始):


为你的类选择一个更好的名称来描述它的用途,不要将它与已经存在的名称混淆。你不应该调用你的类
对象
-这是一个Java超类。为你的类选择一个更好的名称来描述它的用途,不要将它与已经存在的名称混淆。你不应该调用你的类
对象
-这是一个Java超类