• Post Reply Bookmark Topic Watch Topic
  • New Topic

Repeating an action while a condition is true  RSS feed

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!
I want a user to enter his name (may be 50 times or even more)until the name is equal to "Mark".
I have used a do while loop here, but it runs only for two times. It stops execution after 2 times whether user entered or not the intended name correctly.
is not it possible to get the work done using do while loop? or What java theory should I use here?





 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can see at least two big problems with that loop.

1. It keeps looping as long as name is equal to "Mark", not until it's equal to "Mark".

2. Since you never update the name variable in the loop, what you input after the first time is irrelevant.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Well, I can see at least two big problems with that loop.

1. It keeps looping as long as name is equal to "Mark", not until it's equal to "Mark".

2. Since you never update the name variable in the loop, what you input after the first time is irrelevant.

Can you please elaborate this? I did not get you properly.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK...

while(name.equals("Mark"))

What do you think that means? If name is "Mark", it will keep going. That's the opposite to what you said you wanted.

Then, exactly which lines in your code in the main() method do you think the name variable gets updated? I can see only 1 - on line 4. This line:

t1.getName();

gets a String value from the getName() method, but does nothing with it.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:OK...

while(name.equals("Mark"))

What do you think that means? If name is "Mark", it will keep going. That's the opposite to what you said you wanted.

Then, exactly which lines in your code in the main() method do you think the name variable gets updated? I can see only 1 - on line 4. This line:

t1.getName();

gets a String value from the getName() method, but does nothing with it.

yes thanks. How am I gonna fix it?
Googled it many times, but couldn't find the exact solution.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else had a similar problem a few years back: here.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Somebody else had a similar problem a few years back: here.

Thanks will go through it carefully and let you all know if any issue arises
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome Beware: it is not a short thread
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You're welcome Beware: it is not a short thread

yes it is true. Honestly I cannot understand it.
Anybody likes to help me?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't understand a thread, ignore it.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you can't understand a thread, ignore it.

but i want you guys to help me with this problem
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, back to basics. What does this code mean?You can find help in the Java Language Specification, but that can be difficult to read, or in the Java Tutorials.

That is part of your first post, with some lines removed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!