Question

1) Just Initials: Print just the initials in upper case letters separated by periods 2) Last Name First With Middle Initial:

*****Requiremrnt:
1. Please do not use any array or arraylist
2. collects only a single String input
3. uses at least 3 String methods

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//StringOperations.java
import java.util.Scanner;
class StringOperations
{
   //function to get initials
   public String getInitials(String name)
   {
       //split the name at space
       String[] split = name.split(" ");
       int len = split.length;
       String res = "";
      
       //if the length of the splitted name contains more than 3 parts are less than 0 print error message and return
       if(len<=0 || len >3)
       {
           System.out.println("Error Occured: Name should only contains first name, middle name and last name");
           res = "None";
           return res;
       }
      
       //convert each first letter of splitted part to upper case
       //add it to result
       for(int i = 0;i<len;i++)
       {
           res += Character.toUpperCase(split[i].charAt(0)) +".";
       }
       return res;
   }
  
   //function to get first and last and then middle initial
   public String getLastFirstAndMiddle(String name)
   {
       //split the name at space
       String[] split = name.split(" ");
       int len = split.length;
       String res = "";
      
       //if the length of the splitted name contains more than 3 parts are less than 0 print error message and return
       if(len<=0 || len >3)
       {
           System.out.println("Error Occured: Name should only contains first name, middle name and last name");
           res = "None";
           return res;
       }
      
       //convert first letter of each part to upper case and remaining part to lower case
       for(int i = 0;i<len;i++)
       {
           split[i] = Character.toUpperCase(split[i].charAt(0)) + split[i].substring(1).toLowerCase();
       }
       //obtain pattern based on name given according to question
       if(len == 1)
       {
           res = split[0];
       }
       else if(len == 2)
       {
           res = split[1] +", " + split[0];
       }
       else if(len == 3)
       {
           res = split[2] +", "+split[0]+" "+getInitials(split[1]);
       }
       return res;
   }
   public String getFirstAndLast(String name)
   {
       //split the name at space
       String[] split = name.split(" ");
       int len = split.length;
       String res = "";
      
       //if the length of the splitted name contains more than 3 parts are less than 0 print error message and return
       if(len<=0 || len >3)
       {
           System.out.println("Error Occured: Name should only contains first name, middle name and last name");
           res = "None";
           return res;
       }

       //if the length of the splitted name contains more than 3 parts are less than 0 print error message and return
       for(int i = 0;i<len;i++)
       {
           split[i] = Character.toUpperCase(split[i].charAt(0)) + split[i].substring(1).toLowerCase();
       }
      
       //obtain pattern based on name given according to question
       if(len == 1)
       {
           res = split[0];
       }
       else if(len == 2)
       {
           res = split[0] +" " + split[1];
       }
       else if(len == 3)
       {
           res = split[0] +" "+split[2];
       }
       return res;
   }
   public static void main(String[] args)
   {
       StringOperations tester = new StringOperations();
       Scanner inp = new Scanner(System.in);
       System.out.print("What are your first, middle and last names? ");
       String name = inp.nextLine();
       System.out.println("Initials : " + tester.getInitials(name));
       System.out.println("Last Name First : "+ tester.getLastFirstAndMiddle(name));
       System.out.println("First and Last : "+ tester.getFirstAndLast(name));
   }
}
//output and code images

File Edit earch View Document Project Build Iools Help New Open StringOperations.java X 압 ave Save All Revert CloseBack Forwa File Edit earch View Document Project Build Iools Help New Open StringOperations.java X ave Save All Revert CloseBack ForwardFile Edit earch View Document Project Build Iools Help New Open StringOperations.java X 압 ave Save All Revert CloseBack ForwaC:Windows SYSTEM32cmd.exe hat are your first, middle and last names? saLLY rache. DE Initials : S.R.R Last ane Pirst iHeKSC:\Windows SYSTEM32\cmd.exe hat are your first, middle and last names? ada lovelace Initials Last Name First Lovelace. Ada Fi

Add a comment
Know the answer?
Add Answer to:
*****Requiremrnt: 1. Please do not use any array or arraylist 2. collects only a single String...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE...

    *MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE DIRECTIONS BELOW* Write a Java class that takes a full name (first and last) as inputted by the user, and outputs the initials. Call the class Initials. The first and last names should be entered on the same input line i.e. there should be only one input to your program. For example, if the name is Jane Doe, the initials outputted will be J...

  • A person's initials consist of the first letter (in upper case) of each of their names....

    A person's initials consist of the first letter (in upper case) of each of their names. For example, the initials of Alan Mathison Turing are AMT. Use loops to print out all possible initials for people who have exactly three names. Cycle through the letters of the first initial first, followed by the middle initial, and then the last initial. Python Programming A person's initials consist of the first letter (in upper case) of each of their names. For example,...

  • use Java and it must work for any name String Manipulator Write a program to manipulate...

    use Java and it must work for any name String Manipulator Write a program to manipulate Strings. Store your full name into one String variable. It must include first name, middle name, last name, cach separated by spaces. Spaces could vary. If you do not have a middle name make up one For example the string to be processed could be any of the following John Plain Doe John Plain Doc John Plain Doe Your program must be able to...

  • Hi I how do i use a vector to search a array for a string that...

    Hi I how do i use a vector to search a array for a string that user has enter into the console. here is some of the instructions for the programing I am trying to work on The purpose of this program is to write a class whose job is to read up to 10,000 names into a string array. The class sorts the names and then the user can ask the class to find full names containing a name...

  • Write a Python program to create userids: You work for a small company that keeps the...

    Write a Python program to create userids: You work for a small company that keeps the following information about its clients: • first name • last name • a user code assigned by your company. The information is stored in a file clients.txt with the information for each client on one line (last name first), with commas between the parts. In the clients.txt file is: Jones, Sally,00345 Lin,Nenya,00548 Fule,A,00000 Your job is to create a program assign usernames for a...

  • 1. Create a NetBeans project under the name PlayingWithJavaStrings. 2. In the source code do the...

    1. Create a NetBeans project under the name PlayingWithJavaStrings. 2. In the source code do the following: i) Create two separated String variables, one for the first and the other for the last names and set them to “John” and “Smith”, respectively. ii) Create a String variable for the full name and then concatenate the first and last name variables into it. 3. Using the String methods found in the Java API: Print the full name in the terminal window...

  • Python code. No use of def() or return. 7a)Create a program that asks the user for...

    Python code. No use of def() or return. 7a)Create a program that asks the user for their full name (first, middle, and last). You can assume that compound names, such as Jamie-Lynn are joined by a hyphen, that names are separated by a space and that the user enters their data in the correct format. The program should then separate the first name, middle name, and last name from the single string entered by the user and save each piece...

  • In Java,enhace the program base on the first one. 1. You are going to design (and...

    In Java,enhace the program base on the first one. 1. You are going to design (and code) a class called Name. The class Name will contain three properties: first, the first name which is a String initial, the middle initial, which is a single character. last, the last name, which is a String The class has three constructors: A default constructor, which accepts no parameters, calls constructors for the first and last name and sets the initial equal to a...

  • High School Assignment: please keep simple, use for loops but preferably no arrays In python, Instructions...

    High School Assignment: please keep simple, use for loops but preferably no arrays In python, Instructions Write a program to input ten book titles and author's names then save them to a file named books.txt. For each book, the user will first input the title, then input the first name, then the last name of the author, all on separate lines. In addition to saving the books in the file, please output, using the print method, the information you've gathered...

  • ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which...

    ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which prompts the user for a string with 3 words each separated by a single space and reads the line into one String variable using nextline(). Extract each word and add them to a new String variable in reverse order with a space between the words. Have the 1st letter of the new string in upper case and all other strings in lower case letter....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT