Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] NEWBIE, best way to loop?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 September 2000 - 11:44 AM

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?

Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 02 September 2000 - 11:52 AM

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.

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 02 September 2000 - 11:52 AM

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.

#4 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 September 2000 - 04:48 AM

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

#5 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 September 2000 - 11:09 AM

...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!

#6 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 September 2000 - 11:55 AM

&& 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


#7 isrking   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 September 2000 - 11:59 AM

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.



#8 Julio   Members   -  Reputation: 116

Like
Likes
Like

Posted 06 September 2000 - 12:02 PM

I''m mean. use goto

JoeMont001@aol.com www.polarisoft.n3.net




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS