Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the content of an object

Tags:

java

When I start my program, it gives me a link to an object, while I want to get the content. Where's my mistake?

I think the problem lies in storage.addRecord(record) in ReaderXls.class.

Result:

 Reading is over 
 Start reading from Storage
 work2obj.Record@2910d926

.

  public class Start {
            public static void main(String[] args) {
                System.out.println("Start reading from Xls");
                ReaderXls read = new ReaderXls();
                Storage storage;
                storage = read.ReadXls("Text1obj",0,1);
                System.out.println("Reading is over");
                System.out.println("Start reading from Storage");
                System.out.println(storage.getRecord(1));
            }
        }

.

  public class Storage
    {
        List<Record> record;
        public Storage(){
            this.record = new ArrayList<Record>();
        }

.

  public Record getRecord(int number){
            return this.record.get(number);
        }    
      }

.

  public class ReaderXls {
  public Storage ReadXls(String sfilename,int firstColumn, int lastColumn){
            Storage storage = new Storage();
            try {      
  Record record = new Record(j, Integer.parseInt(ContentCount), RowContent);
                    storage.addRecord(record);
  }
 }
like image 572
Eldar Avatar asked Dec 20 '22 01:12

Eldar


2 Answers

You should implement the toString method in the Record class to return a string containing the data you want to display.

By default, since your class doesn't implement toString, Object.toString() gets called, which returns <the name of the class>@<the object's hashcode>

like image 124
Raibaz Avatar answered Jan 07 '23 04:01

Raibaz


You need to Override toString method inside Record class to get the content of object. It Returns a string representation of the object.

If you want to represent any object as a string, toString() method comes into existence. The toString() method returns the string representation of the object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Read more about it's implementation from here.

like image 41
Ankur Lathi Avatar answered Jan 07 '23 03:01

Ankur Lathi