Solved

Using variables set inside of if blocks

Posted on 2004-08-05
8
206 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now