• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Problems reading from a .txt file

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Theres my code for this problem. I have it get its input from input.txt (and yes, I've tried using the full path to the file). The contents of the file are:

5
2
7

Just those 3 numbers, each on a different line. When I run the program however, it comes up with the numbers 53, 13, 10, and 50. Writing the output to another file works fine, based on the data the program got.

So, my question is, where the hell are those 4 numbers coming from and why aren't my 3 numbers getting fed in?

[ October 11, 2006: Message edited by: Joe Ess ]
[ October 11, 2006: Message edited by: Joe Ess ]
 
Bartender
Posts: 9559
12
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lucas Laws:

So, my question is, where the hell are those 4 numbers coming from and why aren't my 3 numbers getting fed in?



Those are the ascii codes for the characters you have in your file. Well, 50 and 53 are. 10 and 13 are line feed and carrage return respectively.
It's hard to know where to steer you without knowing your exact requirements. If you just want to read/write numbers, DataInputStream or RandomAccessFile may be better fits since they can read in ints rather than bytes/chars (which is what read() does). If you have to read the numbers out of a text file, you will have to read the number in as a String and convert it.
 
Lucas Laws
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. I figured it was as much, but couldn't figure out how to fix it. I guess I'll read it in as a string and play with it that way, thanks.

I just changed my read() to readLine() and parsed it to an int, so alls well that ends well, thanks for the help.
[ October 11, 2006: Message edited by: Lucas Laws ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!