Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems reading from a .txt file  RSS feed

 
Lucas Laws
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 ]
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!