I have some issues with the passing array values to the constructor. I have a file.txt which contains some lines with values, for example:
peter|berlin|germany|0930295235|foo.
I figured out how to transform the lines into the arrays. But I don't know how to pass the values from arrays to the constructor to transform the arrays into the objects with the array attributes.
Here is the main class:
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());
}
}
}
Here is the class Item:
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;
}
}
I need objects for more work with the data. I appreciate any help.
Object- that's a Java superclass