What changes should i perform in my code so that it could print the whole family Have tried toString, i am only getting null. This is just a pretty simple code soo plss help.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* Created by Alpit on 26-05-2017.
*/
public class Fam {
String father;
String mother;
String sister;
String brother;
String r;
public Fam(String father, String sister, String brother, String mother) {
this.father = father;
this.sister = sister;
this.brother = brother;
this.mother = mother;
}
public String getFather() {
return father;
}
public void setFather(String father) {
this.father = father;
}
}
class add {
public static void main(String args[]) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Object> arrayList = new ArrayList<>();
for (int i = 0; i < 2; i++) {
String f = obj.readLine();
String s = obj.readLine();
String b = obj.readLine();
String m = obj.readLine();
Fam fam = new Fam(f, s, b, m);
arrayList.add(fam);
}
for (Object x : arrayList) {
System.out.println(String.valueOf(x));
}
}
}
I am only getting the address of Object, This question can be considered to be a duplicate of this question but i was not able to understand by the solution provided there.
This is what i tried again
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* Created by Alpit on 26-05-2017.
*/
public class Fam {
String father;
String mother;
String sister;
String brother;
String r;
public Fam(String father, String sister, String brother, String mother) {
this.father = father;
this.sister = sister;
this.brother = brother;
this.mother = mother;
}
public String getFather() {
return father;
}
public void setFather(String father) {
this.father = father;
}
public String toString()
{
return r;
}
}
class add {
public static void main(String args[]) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Object> arrayList = new ArrayList<>();
for (int i = 0; i < 2; i++) {
String f = obj.readLine();
String s = obj.readLine();
String b = obj.readLine();
String m = obj.readLine();
Fam fam = new Fam(f, s, b, m);
arrayList.add(fam);
}
for (Object x : arrayList) {
System.out.println(x.toString());
}
}
}
And this returns null.