Link to home
Start Free TrialLog in
Avatar of MiKKaV
MiKKaV

asked on

Using variables set inside of if blocks

I am fairly new to Java and I am looking for the best way to accomplish the following.

As I understand it if you set a variable, then change it within an if statement, the variable will return to original once you have finished the if statement.  I have previously got around this by moving any if statements to functions and using a "return x", however it does not always seem appropriate to me to create a function for a simple if test.  Is there a way with the following code to use the returned newestLogFile from the if block:

// Set null values
long newestLogFileModified = 0;
File newestLogFile = null;

for (int i=0; i<logfiles.length; i++) {

      // Get the filename
      String logFilename = logfiles[i];
      File currentLogFile = new File(logFilename);
      long currentLogFileModified = currentLogFile.lastModified();

      // Test if current log file is newer than last known newest
      if (currentLogFileModified > newestLogFileModified) {
            newestLogFileModified = currentLogFileModified;
            newestLogFile = currentLogFile;
      }
}

// This following statement will cause an error "newestLogFile cannot be resolved" if the null statements
// above are not set.

System.out.println("Last file modified was: " + newestLogFile.toString());


I should point out that the code to find the newest logfile, while not the most elegant, does appear to work, I just need my result to break out from if loop.

Hope this all makes sense.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> // This following statement will cause an error "newestLogFile cannot be resolved" if the null statements

thats because without them the variable is not defined.
Avatar of MiKKaV
MiKKaV

ASKER

You are correct, my bad.  Whilst compiling this post I have changed the code a bit, and it appears you are correct and the code posted here works OK.  Heres the points!  Do you think this is the most efficient way of performing this operation? (finding the newest file in a directory)

MiKKaV
> Is there a way with the following code to use the returned newestLogFile from the if block:

use break statement which will come out of the loop, you will have the proper value for "newestLogFile".

mb...
I don't think you can make it much more efficient, as you can't really avoid checking all the files.
Avatar of MiKKaV

ASKER

Thanks for your prompt help!
You can also do

File[] logFiles = someDirectory.listFiles();
List logFilesAsList = Arrays.asList(logFiles);
Collections.sort(logFilesAsList, new Comparator() {
      
      public int compare(Object o1, Object o2) {
            File f1 = (File)o1;
            File f2 = (File)o2;
            return (int)(f1.lastModified() - f2.lastModified());
      }
});
File latest = (File)logFilesAsList.get(logFilesAsList.size() - 1);