-2

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.

Alpit Anand
  • 1,213
  • 3
  • 21
  • 37

4 Answers4

2

You can override toString() from Object in your Class Fam.

Shriram
  • 4,343
  • 8
  • 37
  • 64
2

What String.valueOf(object) does is that it calls the toString() method of class Object (in your case it is Fam).

 public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();

}

So you would need to override the toString() method in Fam class like this:

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

.
.
.

@Override
public String toString() {
    return this.father +" "+this.mother +" "+ this.sister +" "+ this.brother;
}

Additionally, you will need to make this change in your main method.

        ArrayList<Fam> arrayList = new ArrayList<Fam>();

This way you will get your object printed. (PS: you can change the return format in toString() method.)

Julina
  • 709
  • 5
  • 13
2

Implement your own toString() method. Do something like this:

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() {
        StringBuilder builder = new StringBuilder();
        builder.append(this.father);
        builder.append(" ");
        builder.append(this.sister);
        builder.append(" ");
        builder.append(this.brother);
        builder.append(" ");
        builder.append(this.mother);

        return builder.toString();
    }


} 

class add {
    public static void main(String args[]) throws IOException {
        Fam family = new Fam("father", "sister", "brother", "mother");

        System.out.println(family.toString());
    }
}

I have implmented toString() here. I have used a StringBuilder to demonstrate how you can create your own method, inserting a space between "father, sister, brother, mother".

Running you get:

father sister brother mother
chocksaway
  • 870
  • 1
  • 10
  • 21
1

Your ArrayList is given a type parameter of Object. This is fine and all, but when you're trying to print the data in the for loop, you'll need to cast x to Fam. It would be easier to declare it like ArrayList<Fam> arrayList = new ArrayList<>(). Also, you can't simply print objects the way you're trying to do it. Object references hold the value of the memory address. You'll need to manually print the data by using the getter methods in your Fam class. You could also override the toString() method to do this if you want.

Flaom
  • 134
  • 11
  • I have changed the list to fam, and overriided the toString(), still getting null. – Alpit Anand May 26 '17 at 07:23
  • Because you're returning a new variable `r` that hasn't even been initialized. In the `toString()` method, you create a string representation of the object. For example, you could put `return father + ", " + mother ", " + sister + ", " + brother` – Flaom May 26 '17 at 07:25
  • 1
    Overriding means you can have your input template not printing just a variable. Moreover you didn't assign the r value anywhere. – Shriram May 26 '17 at 07:25
  • 1
    srsly this is such a stupid dobut, why didnt it come in my mind earlier. Thanks for your help. – Alpit Anand May 26 '17 at 07:27