java programming inputbox to read a full name, use substring to separate first name and last name

Опубликовано: 02 Март 2026
на канале: RioProfessor Liu
2,024
15

Create a Java program that uses an inputbox to ask user to enter his/her full name

(Example: Shin Liu) , the program displays the following message a message box.

Hello, Shin, your last name is Liu
ublic static void main(String[] args) {
String strFullName;

strFullName = JOptionPane.showInputDialog("Enter your full name -- first last");
System.out.println("full name = " + strFullName);
int intSpaceLocation = strFullName.indexOf(" ");
String strFirstName = strFullName.substring(0,intSpaceLocation);
String strLastName = strFullName.substring(intSpaceLocation+1);
System.out.println(strFirstName + " Your last name is " + strLastName);

}