[SOLVED]
Program description: This part of the program is supposed to go through all the files in the directory, add all values in the file from the second line onwards, then recreate the text file with the sum of the previous values as the only value in the file (after the first line, which is the header).
The problem: All seems to go well until halfway through the "findTotal" method's processing. Using dummy print statements, I can see where the variable "val" (defined as everything on the current line past the first character equals 0; when the actual value in the text file is a non-zero number (64 is the value I've been using). I can quite clearly see that the value it should be picking up is not zero by simply opening the text files that are being read.
I have spent several hours trying to figure out a solution and am totally lost as to what the problem could be. This process should work, I have an alternate version of the program where a single selected file is used instead of the entire directory, which runs perfectly. I can't tell where the difference is that could be causing this issue.
There's a significant amount of code involved, but I'll try to cut away what I can to make it easier to understand.
String[] files = dir.list(); System.out.println("Please enter the amount to decay the target files"); decayVal = Double.parseDouble(input.readLine()); for(int i = 0; i < files.length; i++){ findTotal(files[i]); BufferedReader fileData = new BufferedReader(new FileReader(files[i])); fileData.readLine(); String totalString = fileData.readLine(); total = Integer.parseInt(totalString.substring(1)); System.out.println("Total equals " + total); fileData.close(); total -= (total * (decayVal / 100)); File targetFile = new File(files[i]); targetFile.delete(); targetFile.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(files[i], true)); output.write("DKP_"); output.newLine(); output.write("+" + ((int)total)); output.close(); } // For
Where the method "findTotal" is written as follows:
public static void findTotal (String fileName) throws IOException { char operator = ' '; int val = 0; int total = 0; System.out.println(fileName); // Processes information to be stored in the file try { BufferedReader fileData = new BufferedReader(new FileReader(fileName)); String str; fileData.readLine(); while ((str = fileData.readLine()) != null) { operator = str.charAt(0); if(operator == '+'){ val = Integer.parseInt(str.substring(1)); total += val; }else if(operator == '-'){ val = Integer.parseInt(str.substring(1)); total -= val; } // If } // While fileData.close(); }catch (IOException e){ System.out.println("IOException found."); } // Catch File targetFile = new File(fileName); targetFile.delete(); targetFile.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(fileName, true)); output.write("DKP_"); output.newLine(); output.write("+" + total); output.close(); } // Method: findTotal
Something else I've noticed that I hadn't mentioned earlier: the findTotal() method doesn't appear to be executing the following lines of code, as the file is not rewritten between attempts.
File targetFile = new File(fileName); targetFile.delete(); targetFile.createNewFile(); System.out.println(total); BufferedWriter output = new BufferedWriter(new FileWriter(fileName, true)); output.write("DKP_"); output.newLine(); output.write("+" + total); output.close();