String Concatenation Program in java

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:

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");
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Find Average Age
  2. Login

Leave a Comment

Scroll to Top