Java program, int i (i=0) default value is being used despite it increasing by 1 each loop -


i creating java program copy folders new location automatically, created function loop use same function each given folder source , destination. problem function copy first folder new location multiple times instead of copying once copying next folder. folder locations held in string array , specific 1 selected changing value [i]. each time function loops [i] increases loop not select [i] value next folder copy.

is able me this, code working below, thanks.

import java.io.file; import java.io.ioexception; import java.nio.file.files; import java.nio.file.standardcopyoption;  public class application {  static string[] saves = {         "c:\\users\\lucas\\documents\\my games\\halo",         "c:\\users\\lucas\\documents\\my games\\terraria",         "c:\\users\\lucas\\documents\\my games\\borderlands 2",         "c:\\users\\lucas\\documents\\my games\\rocket league" };  private static int = 1;  file source = new file(saves[i]);  static file folder = new file("saves\\");  file dest = new file(string.valueof(folder) + "\\" + source.getname());  private void start() throws ioexception {     makedirectory(folder);     copy(); }  private void copy() throws ioexception {     copyfileusingjava7files(source, dest);     add(); }  private void add() throws ioexception {     i++;     system.out.println("value of = " + i);     system.out.println(""); }  private static void copyfileusingjava7files(file source, file dest)         throws ioexception {      if (!dest.exists()) {         system.out.println("copying files from: " + "'" + source + "'");         system.out.println("");         copyfolder(source, dest);          system.out.println("file copied");     } else {         copyfolder(source, dest);     } }  private static void copyfolder(file source, file dest) throws ioexception {     if (source.isdirectory()) {         if (!dest.exists()) {             dest.mkdir();             system.out.println("directory created :: " + dest);         }          string files[] = source.list();         (string file : files) {             file srcfile = new file(source, file);             file destfile = new file(dest, file);              copyfolder(srcfile, destfile);         }     } else {         if (source.lastmodified() > dest.lastmodified()) {             files.copy(source.topath(), dest.topath(), standardcopyoption.replace_existing);             system.out.println("file copied :: " + dest);         } else {             system.out.println("a newer version exists of: " + "'" + dest + "'");         }     } }  private static void makedirectory(file folder) {      if (!folder.exists()) {         system.out.println("creating directory: " + "'" + folder + "'");         folder.mkdir();          system.out.println("directory created");     } else {         system.out.println("directory exists: " + "'" + folder + "'");     } }  public static void main(string[] args) throws ioexception {     application app = new application();      int l;     (l = 0; l < 3; l++) {         app.start();     } } } 

it doesn't you're ever changing source field after setting initially. you're setting second file, not changing later. incrementing i won't automatically update source because source file.

also, you're starting i = 1. in java, arrays zero-indexed, means first item in array item 0, should starting i = 0 instead.


Comments