Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop doubt

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my code follows asks to enter the pin number,if it matches with the database pin number, it will proceed.otherwise it will tell wrong pin number.now i need my code to ask the pin number three times if it is wrong.i have used while loop.but i dont know where to place the loop exactly.Even this is simple,please help me to solve this.

the above code comes to while if pin is wrong and it is not terminating even if i give the correct pin number second time.please guide where to put the while loop.otherwise i have to use for loop for this?

Thanks
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, when I get input from the user, I use a do-while loop instead of a while-loop because a do-while loop is always executed at least once.

I couldn't compile your code. The following line had four errors:

String pinnumber = channel.getData(file1,timeout1,maxDigits);

The ID channel is not defined.
The ID file1 is not defined
The ID timeout1 is not defined
The ID maxDigits is not defined.

Will you post your entire class file so that I can try out the code and make sure that I'll give you the right answer?
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by preethi Ayyappan:



This is the only occasion where you get the pin number. What happens if the pin number is wrong the first time? You do not retrieve it when it fails validation.


the above code comes to while if pin is wrong and it is not terminating even if i give the correct pin number second time.please guide where to put the while loop.otherwise i have to use for loop for this?


Not sure what you meant by "it is not terminating". Do you see that your while loop simply counts up till it reaches 2 and then leaves? You have to try and get the pin number and perform validation to see if it is correct.

So, yes -- you have to rewrite your code, and you may be better off doing a do-while loop since you have to get the pin number and validate it at least once.

- Anand
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies.i have tried like this.but it is also not working correctly.

Anyone please help me by modifying the code to work fine.
[ March 06, 2008: Message edited by: preethi Ayyappan ]
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you clarify what you mean by "not working correctly". Also, to quote Jerry Maguire, you would be helping us help you if you indent your code and make it more readable (get rid of tabs when you copy-paste).

In any case, try changing your else condition from -
- to simply - (both the checks in the else clause are redundant). That may or may not resolve your problem, because am not sure what exactly it is.

- Anand
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone please help me by modifying the code to work fine.

That's not what we do here. We're happy to help you, guide you, answer your questions, and so forth.

We do NOT write code for you. Please don't ask again.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, that a do while loop is porobably the best way to go here.
If the input is correct you do the requried work if it fails you go around the loop, if its the final attempt and it fails you do the error conditions.

Its always a good idea to think about what you are attempting to do, and then use some "design" notation to work out the details, for small problems pseudo code often helps, something like:



hope this helps
Gavin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic