2012-11-23 49 views
0

我在傳遞數組值給構造函數時遇到了一些問題。我有一個file.txt,其中包含一些帶有值的行,例如:Java將值從數組傳遞到新的對象實例

peter | berlin | germany | 0930295235 | foo。

我想出瞭如何將行轉換爲數組。但我不知道如何將數組中的值傳遞給構造函數,以便將數組轉換爲具有數組屬性的對象。

這裏是主類:

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

我需要的數據更多的工作對象。我感謝任何幫助。

+1

選擇爲你的類一個更好的名字,描述它是什麼,不與已經存在 – codingbiz

+1

你不應該調用類'Object'的一個混淆 - 這是一個Java超 –

+0

什麼是錯誤? – codingbiz

回答

2

我認爲你想要做的是類似於下面的東西,儘管它不完全清楚。

String s[] = strLine.split("\\|"); 
Item item = new Item(s[0], s[1], s[2], s[3], s[4]); 

另外請注意,您已經創建了新的Item對象後,它會立刻幾乎掉出的範圍,所以你需要將其存儲在某種容器您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); 
} 
+0

謝謝,這工作! – Serv0

1

我不認爲你需要這個for循環。

for(String element : s) 
{ 
} 

如果你正在尋找從你應該使用類似的陣列中提取特定String S,

String name = s[0]; 

,但要小心,不要選擇陣列範圍之外的項目。

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

但這個假設的 'S' 是形式5N的大小; n 1,2,3 ...

1
String s[] = strLine.split("\\|"); 
Object item = new Object(s[0],s[1],s[2],s[3],s[4]); 
1

你不應該有一個for循環,只是傳遞每個數組值到Object的構造函數,首先檢查,你有正確數量的值。新的Object已經在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 

哪個輕微的可讀性,哪個數據進入哪個字段

0

由於您知道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]); 
} 

爲了挽救Item item秒且不與環中的下一個元素覆蓋它們,你可以將它保存到您的項目的數組(開始聲明數組):

public static void main(String[] args) { 
    try { 
     ArrayList<Item> items = new ArrayList<Item>(); 
     ... 

然後

for (String element : s) { 
    System.out.println(element); 
    Item item = new Item(s[0], s[1], s[2], s[3], s[4]); 
    items.add(item); 
}