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