Java Program of String Concatenation

Question:

The authority of XYZ gated residential colony wants its residents’ name datum Should be stored in the following format – residents’ name <space> his/her father’s name. Write a program to concat the father’s name to the residents’ name. The name should be validated,on validation, the name should contain only alphabets and space is allowed. If the name is not valid display the message “Invalid name”. If valid string then convert it to uppercase and print it.. 

[Use concat(String s) of the String class.]

Sample Input 1:

Inmate’s name:Aron

Inmate’s father’s name:Terby

Sample Output 1:

ARON TERBY

Sample Input 2:

Inmate’s name:Mary Anto

Inmate’s father’s name:Jose

Sample Output 2:

MARY ANTO JOSE

Sample Input 3:

Inmate’s name:Dev12

Inmate’s father’s name:Terby

Sample Output 3:

Invalid name

Code:

Authority.java

import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Authority
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Inmate's name:");
        String in_name=sc.nextLine();
        
        System.out.println("Inmate's father's name:");
        String fa_name=sc.nextLine();
        
        Pattern p=Pattern.compile("[^a-z ]", Pattern.CASE_INSENSITIVE);
        Matcher m=p.matcher(in_name);
        Matcher m1=p.matcher(fa_name);
        
        if(m.find()==false && m1.find()==false)
        {
           System.out.println(((in_name.concat(" ")).concat(fa_name)).toUpperCase()); 
        }
        else
        {
            System.out.println("Invalid name");
        }
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

Your email address will not be published. Required fields are marked *

x