• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 11
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain more to a Java programming newbie please?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic