How to print the contents of an object

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

I think the problem is 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); } } 
+4
source share
2 answers

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

By default, since your class does not implement toString , Object.toString() is called, which returns <the name of the class>@<the object hashcode>

+7
source

You need to override toString inside the Record class to get the contents of the object. It returns a string representation of the object.

If you want to represent any object as a string, the toString () method appears. The toString () method returns a string representing the object. If you print any object, the java compiler internally calls the toString () method of the object. Therefore, overriding toString (), returns the desired result, it may be the state of the object, etc. depends on your implementation.

You can learn more about this from here .

+4
source

Source: https://habr.com/ru/post/1496095/


All Articles