aspose file tools*
The moose likes I/O and Streams and the fly likes Why isn't this program working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Why isn Watch "Why isn New topic
Author

Why isn't this program working?

Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
Hi, the objective of this program is to scan through my whole system for the minecraft file. Once this file is found it will exit the loop using break and will launch the minecraft application...

However this program keeps cycling through the list of files and doesn't stop at minecraft... It doesn't exit the loop or launch the application. please help

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

You're invoking parseAllFiles() recursively. The break only stops the loop in the current call; it doesn't stop the recursion. I don't know why the exec would fail though. Maybe you could take out all the looping, and just try to get that part working with a known file path.
Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
Greg Charles wrote:You're invoking parseAllFiles() recursively. The break only stops the loop in the current call; it doesn't stop the recursion. I don't know why the exec would fail though. Maybe you could take out all the looping, and just try to get that part working with a known file path.


Okay i have tried what you suggested and it works perfectly which really doesn't make sense... here is the code if you want to see it:



The output of this is:
it exists!

I also tried replacing the code where it checks if it exists with code where it checks if the code contain "minecraft.exe" in the primary example.

I'm going to try slowing down the speed of the thread to maybe Thread.sleep(300), the idea that it may be going too fast for the computer to process is a long shot but I have no other ideas at this point.
Also how would I exit the loop instead of using break?

Thanks for the help


Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
This is resolved now apart from I dont know how to exit the loop... it launched minecraft when changed this:



to this:

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

Oh, of course. I missed that. You were trying to execute the parent directory, not the program.

As far as ending the recursion, you could have parseAllFiles() return a boolean, so something like:

Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
I understand what you are doing but I am struggling to see how I could use this in my main method...
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

There's a loop in your main method, right? Could you use the return value to know when to break out of that loop?
Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
I've tried doing something similar to what you have done and no because the method is being called in the loop;
all that happens is the loop stops in the method when it finds the minecraft app but it does not stop the program or prints out "done". here is the code I currently have:

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

You've replaced the return value with a global variable. OK, given that you're running this as a one-off application that should work. The problem is that do-while outer loop. If you find the file you're looking for, you set proc to false, and then that loop runs forever. Why do you need that outer loop at all?
Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
okay, I took the while loop out and replaced it with an if loop inside the for loop. I needed the while loop there to show when to stop the loop.

I will give you an overview of what the program does now.. Okay so currently ii finds the minecraft app and launches it however from then on it continues with the loop until all the files have been found. And I bascically want to stop the loop as soon as the minecraft app has been found and currently I just do not see how the program isn't working.

here is the cuurent code:


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2365
    
  50
You need to return a value from the method to indicate you have found the file and don't need to keep searching. A boolean value will do.
For example: At the end of the method return true and in the code that runs the app return false. Then in the code that recursively calls the method check the return value and if it is false immediately return false.
Adamz Preston
Greenhorn

Joined: Jan 19, 2013
Posts: 29
Tony Docherty wrote:You need to return a value from the method to indicate you have found the file and don't need to keep searching. A boolean value will do.
For example: At the end of the method return true and in the code that runs the app return false. Then in the code that recursively calls the method check the return value and if it is false immediately return false.


Okay, I have done what I think you asked me to do but it still wont work. The loop just keeps going and I wouldn't mind this if I had an idea of why it might be going wrong but I don't. Everything appears to be programatically correct.

current code:

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2365
    
  50
You have reversed the return value logic to what I suggested which is fine except you haven't reversed the value returned when you find the file (line 41) so the recursion will never end. You need to return true when you have found the file so the if statement on line 29 is true and the recursion unwinds.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why isn't this program working?