Error reading file as integer

C

cyclone771

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();
}
}
 
D

Dave Glasser

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.


(e-mail address removed) 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
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();
}
}

--
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
 
D

Dave Glasser

And PS, look up Integer.parseInt(String s) and String.valueOf(int n)
for info on doing the conversions.

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.


(e-mail address removed) 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
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();
}
}

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top