Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Small Stupid Mistake - Infinite Loop... Help me before i rip my hair out!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xtreemboarder




PostPosted: Wed Jun 13, 2007 5:14 pm   Post subject: Small Stupid Mistake - Infinite Loop... Help me before i rip my hair out!

code:
public void printMyInfo ()
    {
        try
        {
            System.out.println ("Name: " + c.getName () + "\nPhone Number: " + c.getPhoneNumber () + "\nReferred By: " + c.getReferredBy ());
            char chhh = ' ';
            do
            {
                System.out.println ("Would you like to edit any information? (y/n)");
                chhh = in.readLine ().charAt (0);
                if (chhh == 'y' || chhh == 'Y')
                {
                    editMyInfo ();
                }
                else if (chhh == 'n' || chhh == 'N')
                {
                }
                else
                    System.out.println ("Invalid Entry, try again.");
            }
            while (chhh != 'y' || chhh != 'Y' || chhh != 'n' || chhh != 'N');
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
    }

to sum it up, even though chhh is equal to 'n' or 'y'... it keeps looping. I'm sure a complete noob can find the problem here, i guess im an even bigger noob than a complete noob.
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Jun 13, 2007 6:08 pm   Post subject: Re: Small Stupid Mistake - Infinite Loop... Help me before i rip my hair out!

code:
while (chhh != 'y' || chhh != 'Y' || chhh != 'n' || chhh != 'N');

In your while loop, you keep going as long as the input doesn't match any of these choices, therefore you need it to be and statements (&&) and not or statements (||).
HeavenAgain




PostPosted: Wed Jun 13, 2007 8:14 pm   Post subject: Re: Small Stupid Mistake - Infinite Loop... Help me before i rip my hair out!

for a easier understand of the loop, you might want to make a boolean variable
and say, if user clicked y or Y then the boolean is true, same geos for n or N
and if they clicked other key, then boolean is false
and just put it in your do while

example:

boolean exit = true;
do
{
get the answer
if its y or Y then
exit = true
and edit info
if its n or N then
exit = true
else
exit = false
"invalid input"
} while (!exit);

get what i mean? it will be easier to understand how it works when you have a controling variable instead of having a lot of conditions Razz

but of course HellblazerX's method works also
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: