Link to home
Start Free TrialLog in
Avatar of AnthonyCosenza
AnthonyCosenza

asked on

loop problems

Ok this one is stumping me....

I hav a while loop that has an if statement inside it like so....

    while (userIn != null) {         
        if (userIn.startsWith("quit")) {
            break;
        }            
    }

the userIn is a string that is the user input... what i am tryin to figure out is when i run this program which is a TCP p2p client server pair.. all the connect works... but the while loop is there to keep the program goin... if i enter a line other than "quit" and then enter "quit" it doesnt follow the intended path where the while loop breaks and and the program quits (this has been done elsewhere and works if quit is the first of the user input).

Also if i had another statement in there, for example..

    while (userIn != null) {         
        if (userIn.startsWith("quit")) {
            break;
        } else {
            .... extra statement ....
            eg. System.out.println("testing");
        }      
    }
how do i hav it so that it prints that line just once without havin to kill the while loop??

Thanks
ASKER CERTIFIED SOLUTION
Avatar of TrekkyLeaper
TrekkyLeaper
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AnthonyCosenza
AnthonyCosenza

ASKER

Ok, that seems to hav solved one of my problems...

Its stopped the program from printin testin hundreds of times :)

.. but now how do i make it return to the original loop where if the user inputs "quit" it quits
Ah. Well the input statement should be inside the else, but not in the if( first ) block. Sorry about that.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial