AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Brian Ngure
Expertise
Strong in Java. Will be able to answer both in areas of designing and coding. Some knowledge in servlets

Experience
5 years of Java experience, Sun Certified Java Associate and Sun Certified Java Programmer, JDK 1.4

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > CODE for the question I asked previously

Java - CODE for the question I asked previously


Expert: Brian Ngure - 9/10/2009

Question
import javax.swing.JOptionPane;

public class Assign2
{
   // main method begins execution of Java application
      public static void main( String[] args )
   {
          String firstname = JOptionPane.showInputDialog("Please enter your first name");
       do{  
          JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
          firstname = JOptionPane.showInputDialog("Please enter your first name");
       }while(firstname.isEmpty());   
      String lastname = JOptionPane.showInputDialog("Please enter your last name");
      do{
          JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
          lastname = JOptionPane.showInputDialog("Please enter your last name");
      }while(firstname.isEmpty());
      JOptionPane.showMessageDialog(null, "Hello, " + firstname + " " + lastname + ", " +  "Welcome to CIS 226");
   
   }

}


Answer
Hi,

Because you are using do {...} while, the code inside do will run at least once no matter what happens.

Change the code to use a while loop only.

while (firstname.isEmpty()) {
   JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
}

Looks like a University of London CIS question but I could be wrong :)


Brian

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.