• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

random access file

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a program that writes to and reads from a random access file. That is, it's trying to...

The program loops around (it has a menu that runs in a loop until user chooses to quit), and one of the operations is to read from the file what is in there. The file contains records of an ADT I have developed, but when the method returns objects representing each of these records, instead of outputting the String, the int and the double that I know is saved in the file for each record, it outputs the name of the data type of each record, along with some garble starting with an @sign. Does this look familiar to anyone?

I want the method to return the contents of one record to the driver program , but instead what is output from the driver program (using the toString() method on the object passed as a parameter) is something along the lines of: basicRecord@130c19b.

Any idea what I may be doing wrong?

Thanks!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to override the toString() method in your basicRecord class. What you are seeing is the output from the Object.toString() method.
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I followed your advice and it now returns the data in the record. Thanks!

However, there is a new problem now, in that one of the fields is a String variable. Since I wanted fixed length records, I have a method in the basic record class that "pads out" the String so that it fills the fixed length. When the data in the Record object is returned (using the overridden toString method), it displays the empty bytes as well (they look like little squares).

I'll include the relevant methods in a simplified example:

The following method in the driver program steps through each record sequentially (I know, why sequentially? Will write a hashing algorithm for direct access next):


How can I ensure that the readRecord method only returns those bytes in the String that contain actual characters, and not the unsightly little sqares?

Thanks!
[ September 28, 2005: Message edited by: Flo Powers ]
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the answer. Once the byte array has been crammed back into a String with the required size, simply just trim the empty bytes off the end using the trim( ) method.


[ September 29, 2005: Message edited by: Flo Powers ]
[ September 29, 2005: Message edited by: Flo Powers ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic