You are here:

Java/CODE for the question I asked previously

Advertisement


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

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


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

©2012 About.com, a part of The New York Times Company. All rights reserved.