Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Error reading file as integer

Thread view: 
cyclone771@gmail.com - 08 Oct 2005 19:07 GMT
I am beginning programming in Java, and I have created an application
for a counter that adds 1 to a number stored in a file, then displays
the new number.  However, each time I try to run it, it puts a
seemingly random number/letter in the file, and it starts counting from
49.  What can I do to fix it?
Here is the source code:

import java.io.*;

class counterapp {
   public static void main(String[] args) throws IOException {
       File inputFile = new File("counterapp.jvar");
       File outputFile = new File("counterapp.jvar");

       FileReader in = new FileReader(inputFile);
       int x = in.read();
       in.close();
       int y = (x+1);
       System.out.println(y);
       FileWriter out = new FileWriter(outputFile);
       out.write(y);
       out.close();
   }
}
Dave Glasser - 08 Oct 2005 19:38 GMT
It looks like you're confusing text (strings of characters) with
binary integers. You probably start out with a text file you created
containing the character '0' or '1', but when those are read into your
program as an integer, which is what Reader.read() returns, you get
the ASCII value (48 for '0', 49 for '1', etc.)

If you want the integer to be stored in the file as text (i.e. human
readable) then you have to convert from text to integer and
vice-versa. Keep in mind that Reader.read() will only read in one
character of an ASCII text file, so if your file contains the string
"302", then Reader.read() in your program will return the ASCII value
of '3' which is 51.

I would recommend you use a BufferedReader to read your file in with
the readLine() method, and a PrintWriter to write the file, using the
print(String s) or println(String s) method.

cyclone771@gmail.com wrote on 8 Oct 2005 11:07:26 -0700 in
comp.lang.java.programmer:

>I am beginning programming in Java, and I have created an application
>for a counter that adds 1 to a number stored in a file, then displays
[quoted text clipped - 20 lines]
>    }
>}

Signature

Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net

Dave Glasser - 08 Oct 2005 19:43 GMT
And PS, look up Integer.parseInt(String s) and String.valueOf(int n)
for info on doing the conversions.

Dave Glasser <dglasser@pobox.com> wrote on Sat, 08 Oct 2005 14:38:24
-0400 in comp.lang.java.programmer:

>It looks like you're confusing text (strings of characters) with
>binary integers. You probably start out with a text file you created
[quoted text clipped - 40 lines]
>>    }
>>}

Signature

Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net

Roedy Green - 08 Oct 2005 21:40 GMT
>int x = in.read();
this is not how you read a number.  You  read a string, an entire
line, then convert to int.

To learn how to read the string see
http://mindprod.com/applets/fileio.html

for how to convert it to an int see
http://mindprod.com/applets/converter.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.