This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes while loop doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop doubt" Watch "while loop doubt" New topic
Author

while loop doubt

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Nov 18, 2006
Posts: 688

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

Joined: Aug 22, 2006
Posts: 257

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


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Aug 22, 2006
Posts: 257

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

Joined: Oct 02, 2003
Posts: 11170
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while loop doubt
 
Similar Threads
need help
Need help..Please! How to keep a running total...
Need help to solve JDBC problem
Check for invalid file name
Cannot set a String to null?