I am trying to take a number value saved to a .txt
file, convert it to an int
, and then add this int
and another one that is defined within the program. This section seems to be okay, however when I try to save this new value back to the original .txt
file, a weird symbol is appearing.
/*
* @param args the command line arguments
*/
public class TestForAqTablet1 {
public static void main(String[] args) {
int itemval=0;
String itemcount= "3";
try{
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\kyleg\\Documents\\AQ App Storage\\stock\\1~ k\\od.txt" ));
String line;
System.out.println("reading file");
while((line = in.readLine()) != null){
itemval = Integer.parseInt(line);
}
in.close();
}
catch (IOException ex) {
Logger.getLogger(TestForAqTablet1.class.getName()).log(Level.SEVERE, null, ex);
}
//math
System.out.println("previous number "+itemval);
System.out.println("count "+itemcount);
int total = itemval + Integer.parseInt(itemcount);
System.out.println("Total: "+total);
//write
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\kyleg\\Documents\\AQ App Storage\\stock\\1~ k\\od.txt"), StandardCharsets.UTF_8))) {
writer.write(total);
catch (IOException ex) {
// handle me
}
}
}
}
The .txt
file it's reading from contains only the number 0
.
My goal is for this to increase every time the program is run, by the specified number (itemcount
).
This is the error I keep getting:
run:
reading file
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at test.pkgfor.aq.tablet.pkg1.TestForAqTablet1.main(TestForAqTablet1.java:37)
C:\Users\kyleg\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)