Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why isn't this program working?

 
Adamz Preston
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you are doing but I am struggling to see how I could use this in my main method...
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic