Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Matthew Buska
Greenhorn
Posts: 11
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being consistent would help. You write one thing:

then read something else:
 
Matthew Buska
Greenhorn
Posts: 11
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain more to a Java programming newbie please?
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic