Solved

Using variables set inside of if blocks

Posted on 2004-08-05
8
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

749 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