[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using variables set inside of if blocks

Posted on 2004-08-05
8
Medium Priority
?
215 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 750 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

831 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