Solved

loop problems

Posted on 2004-10-08
4
218 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
[X]
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
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

623 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