I can't get out of a while loop!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abrad0189
    New Member
    • Oct 2006
    • 4

    I can't get out of a while loop!

    Anybody able to help me? I can't get out of a while loop! It works, it just will not stop!

    public class getNames
    {
    public static void main( String args[] )
    {
    String message = "Employee's Name Is" ; //set String Name
    String aPrompt = "Enter Employee's Name:";
    String tmp = "";
    String name = JOptionPane.sho wInputDialog( aPrompt );


    while
    ( name != null ){
    if ( name != null)
    JOptionPane.sho wInputDialog( "Enter Employee's Name" );
    else
    JOptionPane.sho wMessageDialog( null, name ) ;
    }


    //display the message to welcome the user by name
    JOptionPane.sho wMessageDialog( null, name );
    } // end main
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by abrad0189
    Anybody able to help me? I can't get out of a while loop! It works, it just will not stop!

    public class getNames
    {
    public static void main( String args[] )
    {
    String message = "Employee's Name Is" ; //set String Name
    String aPrompt = "Enter Employee's Name:";
    String tmp = "";
    String name = JOptionPane.sho wInputDialog( aPrompt );


    while
    ( name != null ){
    if ( name != null)
    JOptionPane.sho wInputDialog( "Enter Employee's Name" );
    else
    JOptionPane.sho wMessageDialog( null, name ) ;
    }


    //display the message to welcome the user by name
    JOptionPane.sho wMessageDialog( null, name );
    } // end main

    Code:
    import javax.swing.*;
    public class getNames
    {
    public static void main( String args[] )
    {
    String message = "Employee's Name Is" ; //set String Name
    String aPrompt = "Enter Employee's Name:";
    String tmp = "";
    String name = JOptionPane.showInputDialog( aPrompt );
    
    //Whatever is controlling your loop should change inside that loop for that loop to terminate
    while( name != null ){//name is controlling the loop
    	//if ( name != null)This is not neccessary. The loop is never entered if name == null
    		JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
    	//else
    
    	name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
    }
    
    JOptionPane.showMessageDialog( null, "Name was null. Exiting.." );
    } // end main
    }

    Comment

    • abrad0189
      New Member
      • Oct 2006
      • 4

      #3
      Actually, I do not need it to just exit, but to print out the names entered.


      Originally posted by r035198x
      Code:
      import javax.swing.*;
      public class getNames
      {
      public static void main( String args[] )
      {
      String message = "Employee's Name Is" ; //set String Name
      String aPrompt = "Enter Employee's Name:";
      String tmp = "";
      String name = JOptionPane.showInputDialog( aPrompt );
      
      //Whatever is controlling your loop should change inside that loop for that loop to terminate
      while( name != null ){//name is controlling the loop
      	//if ( name != null)This is not neccessary. The loop is never entered if name == null
      		JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
      	//else
      
      	name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
      }
      
      JOptionPane.showMessageDialog( null, "Name was null. Exiting.." );
      } // end main
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by abrad0189
        Actually, I do not need it to just exit, but to print out the names entered.
        Explain then how you want the program to behave. The user keeps on inputing names until what (They click cancel?). If you want to print all the names entered then you need to store them somewhere, arraylist, maybe. Is that it?

        Comment

        • abrad0189
          New Member
          • Oct 2006
          • 4

          #5
          Originally posted by r035198x
          Explain then how you want the program to behave. The user keeps on inputing names until what (They click cancel?). If you want to print all the names entered then you need to store them somewhere, arraylist, maybe. Is that it?
          I would like the user to keep entering names until they enter nothing and then all of the names print out in a text box. Do I need to use an array for that?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by abrad0189
            I would like the user to keep entering names until they enter nothing and then all of the names print out in a text box. Do I need to use an array for that?
            What do you mean by textbox? Do you mean a TextArea because then you would need a JFrame as well and you might not need the array. A message box which is probably what you are refering to would work this way without using arrays
            Code:
            import javax.swing.*;
            public class getNames
            {
            public static void main( String args[] )
            {
            String message = "Employee's Name Is" ; //set String Name
            String aPrompt = "Enter Employee's Name:";
            String tmp = "";
            String allNames = "";
            String name = JOptionPane.showInputDialog( aPrompt );
            
            //Whatever is controlling your loop should change inside that loop for that loop to terminate
            while( name != null ){//name is controlling the loop
            	//if ( name != null)This is not neccessary. The loop is never entered if name == null
            		JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
            	//else
            	allNames = allNames + "\n" + name;
            	name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
            }
            
            JOptionPane.showMessageDialog( null, allNames);
            } // end main
            }
            I was only pointing out the possibilities.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              To stop both when cancel is clicked and when nothing is entered, then while loop needs to be
              Code:
              while( name != null ){//name is controlling the loop
              	//if ( name != null)This is not neccessary. The loop is never entered if name == null
              		JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
              	//else
              	allNames = allNames + "\n" + name;
              	name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
              	if(name != null && name.equals(""))
              	name = null;
              }

              Comment

              • abrad0189
                New Member
                • Oct 2006
                • 4

                #8
                You are awesome! Thank you so much!

                Originally posted by r035198x
                To stop both when cancel is clicked and when nothing is entered, then while loop needs to be
                Code:
                while( name != null ){//name is controlling the loop
                	//if ( name != null)This is not neccessary. The loop is never entered if name == null
                		JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
                	//else
                	allNames = allNames + "\n" + name;
                	name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
                	if(name != null && name.equals(""))
                	name = null;
                }

                Comment

                Working...