• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble Repeating Method  RSS feed

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a fairly newbie question. I have a method that allows the user to name two teams in a baseball games. I want the method to repeat itself if the user enters two equal names, however the main is just carrying on with the next methods. I have highlighted where I think the problem is. If anyone could help that would be great. Thanks.

 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mentioned yourself the magic word: repeat. When we want something to repeat, we put it in a loop. In your case, a while loop will be just fine. Try now to think where and how a while loop should go in your code, and in case you find any problems, we are here!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took out the color, and put in code tags. it makes the code MUCH easier to read.

also...you should never compare strings with "==". I guarantee that is not doing what you think it is. use .equals() instead.
 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I took out the color, and put in code tags. it makes the code MUCH easier to read.

also...you should never compare strings with "==". I guarantee that is not doing what you think it is. use .equals() instead.


Thank you both for the quick replies. As soon as I replaced == with .equals the program worked perfectly. Also thanks for the color tip, didn't know that. Thanks!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's worth pointing out that your method uses recursion to repeat, not looping. That's not necessarily wrong, but it's not the conventional way to do it in a situation like this. Recursion is usually used when implementing an algorithm that is defined in terms of itself, such as, "The factorial of a non-negative integer N is 1 for N=0, and is N times the factorial of (N-1) for N>0," but is not usually used to repeat some action that doesn't depend on previous results.

Note that every time the nameTeams() method calls itself like that, you're putting another stack frame on the stack. For a UI piece like this, that shouldn't recurse deeply enough to cause StackOverflowError, but it's possible in theory. Note also that the first call to nameTeams() will be the last one that finishes.

A more conventional approach here would have been a while loop.

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:It's worth pointing out that your method uses recursion to repeat, not looping. That's not necessarily wrong, but it's not the conventional way to do it in a situation like this. Recursion is usually used when implementing an algorithm that is defined in terms of itself, such as, "The factorial of a non-negative integer N is 1 for N=0, and is N times the factorial of (N-1) for N>0," but is not usually used to repeat some action that doesn't depend on previous results.

Note that every time the nameTeams() method calls itself like that, you're putting another stack frame on the stack. For a UI piece like this, that shouldn't recurse deeply enough to cause StackOverflowError, but it's possible in theory. Note also that the first call to nameTeams() will be the last one that finishes.

A more conventional approach here would have been a while loop.



Thanks for the tip!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!