File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes output recursive to text file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "output recursive to text file" Watch "output recursive to text file" New topic
Author

output recursive to text file

John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Good evening,

I am fairly new at java and need help.

I have spent so much time trying to understand how to output my recursive solution to the tower of hanoi problem that I am now having a hard time seeing where I am going wrong.
I would like to output my result into a text file, instead of having this expected resulted in my text file as follows:

1 --> 3
1 --> 2
3 --> 2
1 --> 3
2 --> 1
2 --> 3
1 --> 3

I get this in my text file:
1 --> 3

To me it looks like my code solution is not going through the whole recurtion but I don't know how to fix it.
If somebody could help me, I would really really appreciate it.




Thank you in advance,
John
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19


Basically, you are recursively opening the same file -- which of course, overwrites each other.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Thank you for such a quick response Henri.

May I ask you, how I could avoid writing over it?
And obtain my desired output

Thank you

John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why do you ignore the constructor parameter? How are you starting the run, considering the first question? Why do you overwrite the file each move?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Don't keep opening it.
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
good morning,

I run it with this:


Could somebody please help me with how NOT to keep on re-opening it?

Thank you,

John
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

well, each time you enter your "solveTowers" method, there is a call to open the file. So, move your opening of the file to outside the method.


Never ascribe to malice that which can be adequately explained by stupidity.
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
thank you Fred,
I will try that

John
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
[solution removed]
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Thank you Sridhar Gudipalli,

I now have to study your example so I can comprehend what was done.

John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Sridhar: http://faq.javaranch.com/java/DontBeACodeMill

IMO the point is to help people learn how to learn, not give them answers on a plate.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

David Newton wrote:IMO the point is to help people learn how to learn, not give them answers on a plate.

That's our philosophy, too. I removed the solution, although it may be too late.
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Good evening,

I apologize in my slownest to respond but quite a lot happens in a day, and I like to study and try out what I am given before responding, and I'm really slow at that.

The code that Sridhar Gudipalli provided me with was extremely helpful, and I would like to thank him once again for I spent so much time on this trying to get it to work on my own and really truely couldn't; that's why when I get stuck like that I come and humbly ask the help of the people that knows.

I looked and spent a good amount of time through the code to make sure I understood it, once again thank you for your all's help.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output recursive to text file
 
Similar Threads
Tower of Hanoi algorithm
Recursion
Textboox problem.
question regarding integer type
How does this actually solve Towers of Hanoi?