Solved

loop problems

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 69
Maven Project: Hibernate Dependencies Conflict 10 23
windows explorer path to command prompt 5 45
xampp tool 12 50
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 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