Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loop problems

Posted on 2004-10-08
4
Medium Priority
?
223 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:AnthonyCosenza
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
TrekkyLeaper earned 750 total points
ID: 12264952
I'd say you do something like this:

   boolean first = true;
   while (userIn != null) {        
        if (userIn.startsWith("quit")) {
            break;
        } else {
            if( first ) {
                .... extra statement ....
                eg. System.out.println("testing");
                first = false;
            }
           
        }    
    }

I think that's what you want.
0
 

Author Comment

by:AnthonyCosenza
ID: 12264966
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
0
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 12264992
Ah. Well the input statement should be inside the else, but not in the if( first ) block. Sorry about that.
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 750 total points
ID: 12265232
Modifying trekky's idea

  boolean first = true;
   while (userIn != null) {        
        if (userIn.startsWith("quit")) {
            break;
        } else {
            if( first ) {
                .... extra statement ....
                eg. System.out.println("testing");
                first = false;
            }
           
        }
         //read userIn value
          userIn=bySomeMeansGetValue();    
    }
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question