0

Write a Java Program that prompts the user to enter his/her first name and last name, then displays a message to welcome the user into the class CIS 226. If the user hits Cancel on either first or last name, show the error message:

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");
	   String lastname = JOptionPane.showInputDialog("Please enter your last name");
	   JOptionPane.showMessageDialog(null, "Hello, " + firstname + " " + lastname + ", " + "Welcome to CIS 226"); 
	
	}

}
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by javaAddict
0

Read the API for the method:
JOptionPane.showInputDialog

If you test your code and see what happens when the user clicks "Cancel" you will see that the method returns null as the API says.

So you need to do some checking of the "firstname","lastname".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.