aspose file tools*
The moose likes Beginning Java and the fly likes What's up with a random number like this being displayed 889193265? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's up with a random number like this being displayed 889193265?

Matthew Buska
Greenhorn

Joined: Jul 27, 2004
Posts: 11

Hi to anyone who reads this I am trying to build 2 programs. The 1st program, called MailOrderWrite, is suppose to allow you to enter data and save it to a file called info.txt which seems to work fine as far as to my knowledge about Java programming. The 2nd program, called MailOrderRead, is suppose to grab the information from the txt file and display it appropriately in the two boxes. That is where I seem to be having my problem. Its displaying an odd result in only 1 of the boxes like 889193265. If anyone can help me I would appreciate it.





[ August 18, 2004: Message edited by: Matthew Buska ]

[ edited to break long lines and to remove the evil tab character -ds ]
[ August 20, 2004: Message edited by: Dirk Schreckmann ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

Being consistent would help. You write one thing:

then read something else:


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Matthew Buska
Greenhorn

Joined: Jul 27, 2004
Posts: 11

Can you explain more to a Java programming newbie please?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

You write a String to file. Lets say "123". It gets stored as a bunch of bytes (file contents in hex): 0003313233
You then try to read an int out of that file. Java doesn't care that you originally wrote a String. It's going to do what you tell it to do. Int's in Java are 32 bits, so we grab the first 32 bits out of the file: 00033132 hex or 209202 decimal. There you have it. A big weird number. But not random. It's what you asked for.
So use the same read/write methods. Either read/writeUTF or read/writeInt. And get familiar with the
Java Tutorial. In this case, the Chapter on DataInputStream would be particularly useful.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I guess readInt doesn't do what you think it does.

It does *not* read a String and parses it to an int.

It *does* instead read a fixed number of bytes and interpretes their bits as an int. That is, it can only read something that was written by writeInt beforehand.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: What's up with a random number like this being displayed 889193265?