• Post Reply Bookmark Topic Watch Topic
  • New Topic

output recursive to text file  RSS feed

 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't keep opening it.
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Fred,
I will try that

John
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[solution removed]
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sridhar Gudipalli,

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

John
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!