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"); 
	
	}

}

Recommended Answers

All 2 Replies

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".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.