aspose file tools*
The moose likes I/O and Streams and the fly likes why theobject is not Retrieving the current value from the file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "why theobject is not Retrieving the current value from the file?" Watch "why theobject is not Retrieving the current value from the file?" New topic
Author

why theobject is not Retrieving the current value from the file?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

what i want to do we serialization is that ...the default value of balance is o , so whenever the code is running it takes the input from the balance owner
so that every time the owner input certain amount into his balance from commandline his balance should keep on increasing ....
like
from 0
after input of 100 it should be 100 then after input of 200 it should be 300 , but from where the code will take the previous 200 it will take from the object which i have saved in some file on my disk
but this is not working in this way , every time i enter amount say 100 it shows me 100 but if i again run the code and enter say 100 it should show me 200
but it is showing 100 only because it is not Retrieving the values of balance from that object which i saved in the file.
why so ???




The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

I'm not exactly sure what your problem is. I do object to your using a "set" method to increment a value, but that's a readability issue, not a functional one. Are you saying that the second time you run this program, you want it to remember the previous value it saved on the first run and increment that? It's not doing that because the first thing you do is create a brand new Demo object. What you may want to do is check if there is an existing file first. If there is, then you read it in, but if not you create a new Demo object. That way you will be working with the old saved value if there is one.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Greg Charles wrote:. Are you saying that the second time you run this program, you want it to remember the previous value it saved on the first run and increment that?

yes , exactly like bank account balance everytime you deposited money your balance keeps on increment right ?
i didnt get what you have given me a solution , are you saying me that i should delete that file and start all over it again ?
or because i have file in my drive which contains object and i m reading from that file only in my code .
but still it is not giving me proper output , i request you first run this code so that so you will have better understanding about the logical error in my code .
thanks
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

No, don't delete the file if you want to retrieve the value you saved in a previous run.

Here's what your program does:

Creates a brand new Demo object with 0 balance
Gets a value from the user and increments the Demo balance by that amount
Writes out the Demo object to a file, writing over what was there before
Reads the Demo object back from the file


So, you've got all the right pieces. You just need to change the order around. Read the object from the file before you write over it.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Greg Charles wrote:No, don't delete the file if you want to retrieve the value you saved in a previous run.

Here's what your program does:

Creates a brand new Demo object with 0 balance
Gets a value from the user and increments the Demo balance by that amount
Writes out the Demo object to a file, writing over what was there before
Reads the Demo object back from the file


So, you've got all the right pieces. You just need to change the order around. Read the object from the file before you write over it.

i have tried by changing the order too

now the problem is with addition part
initaillly it is zero right ?
after i enter 100 it should show me 100 and after that if i enter one more 100 it should show me 200 right because(100 + 100 = 200)
but it is showing me some other values like
suppose after zero i added 200
and after that i have added 20 so logically now i have 220 balance right ? but have a look at the output beneath
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

What is going on in this method?



[How To Ask Questions On JavaRanch]
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

I hate to see you getting so frustrated over this. When I get stuck on something, I try to break the problem down into baby steps. As Joe pointed out, your totBal() method is probably not doing what you intended. What if you forget about serialization for the moment and concentrate on getting the Demo class right? Call totBal() then call getBal(). Do the results make sense? Try it a few times. Is there a pattern to them? Follow the code through line by line. Why is it doing what it does?

When you get back to serialization, you have to read the Demo object from the file and do something with it. Your code now reads Demo into "one", prints the balance, and then abandons it. You are calling totBal() on d, a completely different instance of Demo.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Joe Ess wrote:What is going on in this method?



ya i got problem was i was using different references for retrieving the value
now i have fixed it
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why theobject is not Retrieving the current value from the file?