• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] NEWBIE, best way to loop?

This topic is 6439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I posted this before... what''s the best way to wait till the user puts valid input in? java has no goto statement right?? so what is the best way to go back to the beginning of the method and get input again?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You could set up the method that gets the input as a thread so that is was called every so many fractions of a second.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could set up the method that gets the input as a thread so that is was called every so many fractions of a second.

Share this post


Link to post
Share on other sites
This is a REALLY newbie question.

You are talking about goto. goto is a no no in any modern language and even if it was present you shouldn't use it.

NEWBIE questions diverse simple answers,
so to check input the way you describe write:

do
{
< call function that retrieves input >
}
while ( <input is invalid> )

The loop will exit when the input is valid.
Put your own code within the < >.

Jacob Marner


Edited by - felonius on September 3, 2000 11:50:04 AM

Share this post


Link to post
Share on other sites
...sorry, your soo right?

I also have confusion too
&& is and right?

so i can do { getnumber}
while {inputISvalid() != true && counter != 10}



am i right in inputISvalid() != true
inputISvalid returns true if it''s valid input
so I shouldn''t go through the loop again if it''s true right?

the && counter != 10 is also evaluated

say it''s not valid input and the counter is 10?
does it loop again? I don''t think it should.

Thanks alot felonius!

Share this post


Link to post
Share on other sites
&& means logical AND and you use it correctly.

A && B.

Don''t mix it up with & which is something else entirely.

If A is true then B is evaluated (otherwise not)
and if B also is true then the complete
expression is true.

Note: because inputISValid() returns a boolean
it is already a boolean so you could rewrite
inputISvalid() != true
as
!inputISvalid()

When the expression in the while() is false
the loop ends and are not evaluated any more.

So the loop will end when the input is valid or
when then counter reaches 10.

There is a variation:
while ( )
{

}

While the do{}while() structure always run the
statements in the {} at least once and then does the
test the strcuture above makes the test before it
enters the first time.

Jacob Marner

Share this post


Link to post
Share on other sites
Do while and while loops are the best ways to wait for valid input in any language. For the loop to be executed both conditions must be true when using the double ampersand.

I personally prefer the while loop to the do while, but you can go either way. Even if there was a goto available in Java it wouldn''t be good to use it. In most cases using goto statements leads to awful code. Avoid them whenever possible.

Adam

P.S. Don''t worry about asking a newbie question. We all start out as newbies.

Share this post


Link to post
Share on other sites

  • Advertisement