Click here to Skip to main content
15,664,272 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For Example this is my code:

String fullName; // Stores fullName as a String variable

fullName = JOptionPane.showInputDialog(null,"What is your full name?");
// Creates input dialog box containing "What is your full name?"

JOptionPane.showMessageDialog(null, "You entered: " +

I want to display the first name in its own line the last name in another line.

What I have tried:

I've tried using "\n" inside the message box, but it just takes the whole full name to a new line.
Updated 29-Oct-17 13:02pm

1 solution

You have to manipulate the fullName variable before displaying it.
Something along:
fullName = JOptionPane.showInputDialog(null,"What is your full name?");

fullName = fullName.trim().replaceFirst("\\s+", System.lineSeparator()); // Replace the first whitespace(s) encountered by an end-of-line character

JOptionPane.showMessageDialog(null, "You entered:" + System.lineSeparator() + fullName);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900