0

So I'm working on a program to interface with a file based database. Mostly I'm trying to figure out how to work with it so that I can make objects and store their information in the database so that I can pull the data later.

IE Object Taylor
Name = Taylor
Age = 20
School = Whatever

So that I can get back on and call that information up when queried.

This is an example of an object I want to store. I may be doing this part wrong. package com.catalyse.db;

public class Taylor implements java.io.Serializable
{
    public String name = "Taylor M May";
    public int age = 20;
    public String school = "UC Boulder";
}

The DB structure I'm using is based on RandomAccessFile and I didn't make it, I'm just trying to figure out how to implement it.

package com.catalyse.db;

import java.util.*;
import java.util.Scanner;


/**
 * Simple test class for the RecordsFile example. To run the test, 
 * set you CLASSPATH and then type "java hamner.dbtest.TestRecords"
 */
public class Run {

  static void log(String s) {
    System.out.println(s);
  }

  private static String name()
  {
      Scanner name = new Scanner(System.in);
      String name1 = name.next();
      return name1;
  }

  public static void main (String[] args) throws Exception {

    System.out.println(new Date()); 

    Scanner SC = new Scanner(System.in);

    log("What would you like to name the database?");

    String filename = SC.next();

    log("creating records file...");

    RecordsFile recordsFile = new RecordsFile(filename+".records", 64);

    log("adding a record...");
    RecordWriter rw = new RecordWriter("foo.username");
    rw.writeObject(new Taylor());
    recordsFile.insertRecord(rw);

    log("reading record...");
    RecordReader rr = recordsFile.readRecord("foo.username");
    Taylor name = (Taylor)rr.readObject();
    System.out.println("\tlast access was at: " + name.toString());

    log("test completed.");
  }
}

And here is what I get back from it,

Wed Nov 20 11:56:04 MST 2013
What would you like to name the database?
save3
creating records file...
adding a record...
reading record...
    last access was at: com.catalyse.db.Taylor@50aed564
test completed.

My problem is that I want it to return information about the class, not just its name and location in the DB.

2 Answers 2

3

You need to override the toString method.

public String toString()  
{  

   StringBuilder sb = new StringBuilder();  
   sb.append("Name: ");  
   sb.append(this.name);  

   //rest of fields 
   return sb.toString();
}  

As a matter of clarity, you are not returning its location in the database. You are getting back the object hashValue + the class name.

Sign up to request clarification or add additional context in comments.

2 Comments

I like the answer from @ross better because I can pull individual pieces from it, what is the benefit of the toString method. Also is there a way that I can list out the various pieces in the object without knowing them. So if Taylor.age Taylor.school and Taylor.home existed in the object in storage, how do I get it to list what info the object has?
toString() is supposed to return a String representing the objects important members, i.e. a String representation of the objects state. There are ways of using something called Reflection to get lists of members but that's way beyond the scope of this question. If an answer was helpful, please accept it. :)
0

At this point

Taylor name = (Taylor)rr.readObject();

You can access whatever information you like in the object, e.g.

Taylor name = (Taylor)rr.readObject();
System.out.println(name.age + ", " + name.name + ", " + name.school);

Alternatively, just add a

public String toString() 
{
   return name + ", " + age + ", " + school;
}

method in Taylor and then output it like so

Taylor name = (Taylor)rr.readObject();
System.out.println(name);

Now, concerning...

System.out.println("\tlast access was at: " + name.toString());

name.toString() isn't really required. If you append an object to a String then it automatically calls that objects toString() method to get a value.

Lastly, I'd like to note that generally we don't access object members like name, school and age by just accessing them. We generally make them private members then add methods to get and set them, so that we control and can track how they are manipulated.

1 Comment

i wouldnt recommend this approach. toString is the standard function to override.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.