Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

loop problems

Posted on 2004-10-08
4
216 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 250 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 250 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
eclipse shortcuts 9 62
Unhandled exception type Exception 18 42
Convert from a json string array to a Java object 3 51
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

839 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