- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to print the initials of a name with last name in full
When the full name is provided, the initials of the name are printed with the last name is printed in full. An example of this is given as follows −
Full name = Amy Thomas Initials with surname is = A. Thomas
A program that demonstrates this is given as follows −
Example
import java.util.*; public class Example { public static void main(String[] args) { String name = "John Matthew Adams"; System.out.println("The full name is: " + name); System.out.print("Initials with surname is: "); int len = name.length(); name = name.trim(); String str1 = ""; for (int i = 0; i < len; i++) { char ch = name.charAt(i); if (ch != ' ') { str1 = str1 + ch; } else { System.out.print(Character.toUpperCase(str1.charAt(0)) + ". "); str1 = ""; } } String str2 = ""; for (int j = 0; j < str1.length(); j++) { if (j == 0) str2 = str2 + Character.toUpperCase(str1.charAt(0)); else str2 = str2 + Character.toLowerCase(str1.charAt(j)); } System.out.println(str2); } }
Output
The full name is: John Matthew Adams Initials with surname is: J. M. Adams
Now let us understand the above program.
The name is printed. Then the first letter of the name is printed i.e. the initials. The code snippet that demonstrates this is given as follows −
String name = "John Matthew Adams"; System.out.println("The full name is: " + name); System.out.print("Initials with surname is: "); int len = name.length(); name = name.trim(); String str1 = ""; for (int i = 0; i < len; i++) { char ch = name.charAt(i); if (ch != ' ') { str1 = str1 + ch; } else { System.out.print(Character.toUpperCase(str1.charAt(0)) + ". "); str1 = ""; } }
Then, the entire surname of the name is printed. The code snippet that demonstrates this is given as follows −
String str2 = ""; for (int j = 0; j < str1.length(); j++) { if (j == 0) str2 = str2 + Character.toUpperCase(str1.charAt(0)); else str2 = str2 + Character.toLowerCase(str1.charAt(j)); } System.out.println(str2);
- Related Articles
- Python program to print the initials of a name with last name in full?
- Program to find the initials of a name in C++
- Java Program to get full day name
- MySQL query to display columns name first name, last name as full name in a single column?
- Constructing full name from first name, last name and optional middle name in JavaScript
- Validate the first name and last name with Java Regular Expressions
- Write the full name of ‘SONAR’.
- Update a table in MySQL and display only the initials name in a new column
- Find all users with a unique last name in MySQL?
- Program to print its script name as output in Python
- Java Program to display date with day name in short format
- How to print a variable name in C?
- Java Program to get name of parent directory
- MySQL query to select everything to left of last space in a column with name records
- Program to print its script name as output using Python

Advertisements