• Post Reply Bookmark Topic Watch Topic
  • New Topic

While Loop question?  RSS feed

 
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question : What is the function of this line of code .. Anyone please Explain it

 
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor wrote:Question : What is the function of this line of code .. Anyone please Explain it



Saad Zahoor wrote:Question : What is the function of this line of code .. Anyone please Explain it



I've never seen an instance of Scanner used inside the header of a loop but:

The "!" Means (while the following is not true) do stuff...

Next is a method from class scanner that prompts the user for a string which will be assigned to the variable name. EqualsIgnoreCase then checks whether the string is equal to 'done'.

DoNe would be equal to done. Again, I don't even know if you can use Scanner like that and assign it to a variable in a while loop. That might be a compile error. Where did you even see a statement like that?
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel explained it quite well. Yes, you can take user's input from within the loop.
So yes, what it does, it takes input from the user and does repetition of routine until user enters "done" or "DONE", or any other similar combination of upper and lower cases.
Now is it used there 'while' or 'do while' loops doesn't really matter for the condition itself.

Daniel, have a cow for a nice contribution.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another cow? Would 2 cows constitute officially as a herd?

So....would asking for input from the user inside the header of a while loop be an example of good refactoring? Putting the user input inside the loop along with a control statement to see whether or not is equal to the string (with a boolean in the header of the while loop) is what I've seen many times explained in introductory books. But I've never seen it like that.

 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question :    why this line give Error while number is int


 
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
Saad Zahoor wrote:Question :    why this line give Error while number is int




Primitive int types are not objects. You can't dereference it to call methods. .... ie. there is no equalsIgnoreCase() method, as there is no such thing as methods with that type.

Henry
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives an error because the equals() and equalsIgnoreCase() are methods from the class String. Int is it a string. Therefore you should use == to check whether it is equal to another number.

The == is an equality operator.
The = an assignment operator
(These may not be the actual names of it but that's what they do)

Note that if you use == to compare a string you would be checking to see if it references the same address in memory. So always use the equals() methods when comparing strings and == for ints
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another problem it work with the String but the output is not as it should be .. Output Below

 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, isn't equalsIgnoreCase a method of the class String?

Also, I am not an expert at explaining certain things in java even if I believe I know how they work but I also like to help. I can contribute with an answer that might help someone out every now and then!

Is this allowed or should we only help as long as we are a 100% sure that what we are saying is correct?
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I think you meant there's no such thing as a method with that name for ints. Never mind.
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:
Also, I am not an expert at explaining certain things in java even if I believe I know how they work but I also like to help. I can contribute with an answer that might help someone out every now and then!

Is this allowed or should we only help as long as we are a 100% sure that what we are saying is correct?
Your contribution is very welcome. We all do mistakes from time to time (some of us more often) and give a bit misleading advice or not quite correct so to speak, but we don't do that on purpose, in one or another way we all learning - some learning how to teach, some just learning, some doing other stuff behind the scenes.

Luckily here are tons of well experienced guys, so be assure you'll be corrected once you slip over the mechanics.
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:It gives an error because the equals() and equalsIgnoreCase() are methods from the class String. Int is it a string. Therefore you should use == to check whether it is equal to another number.



Thanks .. Now my problem is reducing !!


now the problem is down

 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor, I re-read the thread and realised, that you have never described the problem in general, what you are trying to solve?
It seems we're fixing things here rather than solving something. Please explain about your problem so somebody could guide you towards a right direction - the practical labour part is down to yourself to accomplish.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm understanding correctly you want to avoid adding a zero in the list before hand instead of adding it and removing it later?

Then your list should be inside the loop. If the user inputs a zero, check for it with an if statement and if it is add zeroCount++ until it reaches 3 zeros.

If is not a zero continue adding the numbers to the list. I'm not sure if this is what you're asking
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Daniel Andres wrote:
Your contribution is very welcome. We all do mistakes from time to time (some of us more often) and give a bit misleading advice or not quite correct so to speak, but we don't do that on purpose, in one or another way we all learning - some learning how to teach, some just learning, some doing other stuff behind the scenes.

Luckily here are tons of well experienced guys, so be assure you'll be corrected once you slip over the mechanics.


Perfect, thanks!
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question :   my problem is -- >  while loop will add values to the List and it will break the loop when i enter zero . Okay !! But zero will also store into the list For that purpose  i add (value.remove(0)) to remove Value from the List !! .. That's Easy . But the question is some One on this Site told me that i should use  [ while (!(name = myScanner.next()).equalsIgnoreCase("done")) ]  for better So i am asking what this line do .


It gives stupid Error Like if i put 6 values and enter 0 to break the loop it give me only 3 values

OUTPUT





So : so should i ask stupid Question or use this line

 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Line 24 is adding the numbers only. If your first number is 95 can you see where your next number goes when you enter the while loop?
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:
Line 24 is adding the numbers only. If your first number is 95 can you see where your next number goes when you enter the while loop?


Don't know why it did that. Im sure I highlighted the right part of the code. I meant line 24:
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it enter into the List .. What it has to do with count .. Count only indicate that how much number or entries i put .?
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry can't help out more I have to go to work. I'm sure someone will guide you towards enlightenment soon enough.
I wish we were able to edit our own posts.
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Problem !!!
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you see a pattern how every second number gets discarded without adding it to your list?
Well, it seems you're using do-while loop. So what happens is that you read in within the body number gets added, then within while condition you read one more number, checking if he isn't "done" or 0 or whatever and then you're not doing anything with it, then asking user enter number again, and taking another number as input in effect discarding previous one.

Try to use while loop instead do-while. It might will work for you.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:Another cow? Would 2 cows constitute officially as a herd? . . .
Only if one is really a bull and you wait a bit...
And congratulations on the cattle.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strange‑looking code inside the loop header is much more frequently used with buffered readers. A buffered reader returns null when it reaches the end of its source:-That works exactly as before. You read a line, which has to be in () because = has a lower precedence than !=. Then you test whether line was null; if so you can terminate the loop. In the case of a Scanner, you would usually use hasNext() (or hasNextLine()) for a Scanner:-In both cases line 1 is try with resources, which ensures the resource is closed without needing a finally block.
Using !...equals() on a finite source is hazardous; if you reach the end of the source before reaching the sentinel value for terminating the loop, there is a risk of an Exception. So you would be better off with something like this, unless you are dealing with an endless stream as a source (System.in is such an endless stream as long as you don't try to close it):-You must use && not & and when you reach the end of the input, the loop will terminate without exceptions.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!