Learn from this program in Java programming language uses functions of String class to demonstrate string operations on a sample registration info.
Updated: Sept. 25, 2023 — Training Time: 3 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: String Operations - Java Programming
Difficulty: Easy.
Companies who previously asked this: -.
Objective: Bonus - String Operation.
Java function that receives and validates an email
Java function that returns first name and last name in an array of String.
Java function that receives first name and last name and returns full name.
Java function that finds the total length of the full name.
Java function that checks whether the two names(first name and last name) are identical.
Java function that displays the names in many formats
Input: Name, email.
Expected Output: Result will vary depending on the user input.
Sikademy Solution
package sikademy;
/**
*
* @author Archangel Macsika
*/
import java.util.Scanner;
// This class uses functions of String class to operate on strings.
public class UsingStringFunctions {
private String initial = null;
private String firstName = null;
private String lastName = null;
private String email = null;
private String FullName = null;
// This function asks the user for the name...
private void promptName(){
Scanner scan=new Scanner(System.in);
System.out.println("Enter the initials: Mr/Mrs/Ms.");
initial=scan.next();
if(initial.equalsIgnoreCase("Mr")|| initial.equalsIgnoreCase("Mrs")|| initial.equalsIgnoreCase("Ms")){
System.out.println("Enter the First Name:");
firstName=scan.next();
System.out.println("Enter the Last Name: ");
lastName=scan.next();
}
else{
System.out.println("Please enter valid initials");
promptName();
}
}
private void promptEmail(){
Scanner scan=new Scanner(System.in);
System.out.println("Enter your Email address:");
email=scan.next();
}
// This function returns the full name...
private String fullName(){
FullName=firstName+" "+lastName;
return (initial.toUpperCase()+"."+" "+firstName.toUpperCase()+" "+lastName.toUpperCase());
}
// if valid email returns true else returns false
private boolean checkEmail(){
if(email.matches("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b")){
return true;
}
else{
return false;
}
}
// This function returns first name and last name in an array of String.
private String[] split_first_Last_Name(){
String[] f_L_name=FullName.split(" ");
return f_L_name;
}
private int findindex(String name_type){
int i=FullName.indexOf(name_type);
return i;
}
// This function finds the total length of the full name..
private int find_Length(){
return(FullName.length());
}
// This function checks whether the two names(first name and last name) are identical...
private int compare_Names(){
return(firstName.compareTo(lastName));
}
// This function displays the names in many formats
private void display(){
System.out.println("Your Full Name is : \t " + fullName());
if(checkEmail()){
System.out.println("Your Email Address is : \t" + email);
}
else{
System.err.println("Your Email Address is not Valid...");
}
String[]names = split_first_Last_Name();
System.out.println("Your First Name is : \t" + names[0]);
System.out.println("Your Last Name is : \t" + names[1]);
System.out.println("Your First Name is at index: \t" + findindex(firstName));
System.out.println("Your Last Name is at index: \t" + findindex(lastName));
System.out.println("The total Length of the Full Name is: \t" + find_Length());
int value = compare_Names();
if(value == 0){
System.out.println("The First Name and Last Name are both equal");
}
else if(value < 0){
System.out.println("The Last Name is greater than First Name");
}
else if(value > 0){
System.out.println("The First Name is greater than Last Name");
}
else{
System.err.println("Error!!!");
}
}
// This is the main class
public static void main(String[] args) {
UsingStringFunctions sf=new UsingStringFunctions();
sf.promptName();
sf.promptEmail();
sf.display();
}
}