Solved

Using variables set inside of if blocks

Posted on 2004-08-05
8
208 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:MiKKaV
8 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11733035
> 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.

thats not correct
0
 
LVL 92

Expert Comment

by:objects
ID: 11733039
> // This following statement will cause an error "newestLogFile cannot be resolved" if the null statements

thats because without them the variable is not defined.
0
 

Author Comment

by:MiKKaV
ID: 11733055
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
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 6

Expert Comment

by:expertmb
ID: 11733061
> 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...
0
 
LVL 92

Expert Comment

by:objects
ID: 11733064
I don't think you can make it much more efficient, as you can't really avoid checking all the files.
0
 

Author Comment

by:MiKKaV
ID: 11733079
Thanks for your prompt help!
0
 
LVL 92

Expert Comment

by:objects
ID: 11733092
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11738038
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);
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question