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...

  • 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...

  • 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...

  • 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...

  • Do WITHOUT #include<sstream> C++ You are to write a program that will read in upto 15...

    Do WITHOUT #include<sstream> C++ You are to write a program that will read in upto 15 names, rearrange each person's name. The list has been collected from different places and the names are not in some standard format. A person's last name may appear last or it may appeaer first. A label has been associated with each name to identify where the last name appears. Either "surname" or "lastname" appears just before a person's last name. The other will be...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Exercise Five: (Parallel Array) In this exercise create two string arrays. One array will hold your...

    Exercise Five: (Parallel Array) In this exercise create two string arrays. One array will hold your name, another will hold corresponding password. Example: string name [] = {"name1","name2", "name3"}; string password [] = {"password1", "password2", "password3"}; Here for name1, matching password is password1, name2, matching password is password2 and so on.... Please ignore my creativity, you can pick name and password your way and it doesn't have to have just 3 names and password. It can be less/more. I am...

  • JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string...

    JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string of someone's full name and prints it out last name first. You may use the available string manipulation methods. Example: "George Washington" "Washington, George" 2. Write a program that will take a string of someone's full name, and break it into separate strings of first name and last name, as well as capitalize the first letter of each. Example: "joseph smith", "Joseph" "Smith"

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
ADVERTISEMENT