473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.showInputDialog( aPrompt );


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


//display the message to welcome the user by name
JOptionPane.showMessageDialog( null, name );
} // end main
Oct 30 '06 #1
7 4813
r035198x
13,262 8TB
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.showInputDialog( aPrompt );


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


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

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. public class getNames
  3. {
  4. public static void main( String args[] )
  5. {
  6. String message = "Employee's Name Is" ; //set String Name
  7. String aPrompt = "Enter Employee's Name:";
  8. String tmp = "";
  9. String name = JOptionPane.showInputDialog( aPrompt );
  10.  
  11. //Whatever is controlling your loop should change inside that loop for that loop to terminate
  12. while( name != null ){//name is controlling the loop
  13.     //if ( name != null)This is not neccessary. The loop is never entered if name == null
  14.         JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
  15.     //else
  16.  
  17.     name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
  18. }
  19.  
  20. JOptionPane.showMessageDialog( null, "Name was null. Exiting.." );
  21. } // end main
  22. }
Oct 30 '06 #2
Actually, I do not need it to just exit, but to print out the names entered.


Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. public class getNames
  3. {
  4. public static void main( String args[] )
  5. {
  6. String message = "Employee's Name Is" ; //set String Name
  7. String aPrompt = "Enter Employee's Name:";
  8. String tmp = "";
  9. String name = JOptionPane.showInputDialog( aPrompt );
  10.  
  11. //Whatever is controlling your loop should change inside that loop for that loop to terminate
  12. while( name != null ){//name is controlling the loop
  13.     //if ( name != null)This is not neccessary. The loop is never entered if name == null
  14.         JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
  15.     //else
  16.  
  17.     name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
  18. }
  19.  
  20. JOptionPane.showMessageDialog( null, "Name was null. Exiting.." );
  21. } // end main
  22. }
Oct 30 '06 #3
r035198x
13,262 8TB
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?
Oct 30 '06 #4
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?
Oct 30 '06 #5
r035198x
13,262 8TB
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
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. public class getNames
  3. {
  4. public static void main( String args[] )
  5. {
  6. String message = "Employee's Name Is" ; //set String Name
  7. String aPrompt = "Enter Employee's Name:";
  8. String tmp = "";
  9. String allNames = "";
  10. String name = JOptionPane.showInputDialog( aPrompt );
  11.  
  12. //Whatever is controlling your loop should change inside that loop for that loop to terminate
  13. while( name != null ){//name is controlling the loop
  14.     //if ( name != null)This is not neccessary. The loop is never entered if name == null
  15.         JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
  16.     //else
  17.     allNames = allNames + "\n" + name;
  18.     name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
  19. }
  20.  
  21. JOptionPane.showMessageDialog( null, allNames);
  22. } // end main
  23. }
  24.  
I was only pointing out the possibilities.
Oct 30 '06 #6
r035198x
13,262 8TB
To stop both when cancel is clicked and when nothing is entered, then while loop needs to be
Expand|Select|Wrap|Line Numbers
  1. while( name != null ){//name is controlling the loop
  2.     //if ( name != null)This is not neccessary. The loop is never entered if name == null
  3.         JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
  4.     //else
  5.     allNames = allNames + "\n" + name;
  6.     name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
  7.     if(name != null && name.equals(""))
  8.     name = null;
  9. }
Oct 30 '06 #7
You are awesome! Thank you so much!

To stop both when cancel is clicked and when nothing is entered, then while loop needs to be
Expand|Select|Wrap|Line Numbers
  1. while( name != null ){//name is controlling the loop
  2.     //if ( name != null)This is not neccessary. The loop is never entered if name == null
  3.         JOptionPane.showMessageDialog( null, "Welcome "+name ) ;
  4.     //else
  5.     allNames = allNames + "\n" + name;
  6.     name = JOptionPane.showInputDialog( "Enter Employee's Name" );//name is changed here
  7.     if(name != null && name.equals(""))
  8.     name = null;
  9. }
Oct 31 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
23
by: philipl | last post by:
hi, I have some code here which basically look for within a string, the occurance of any 3 consectative characters which are the same. so AAA bbb etc would be reported by this function. I later...
1
by: C-- | last post by:
for (expression1; expression2; expression3) {statement} can you explain it to me...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.