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 :)