Java || Whats My Name? – Practice Using Strings, Methods & Switch Statemens

Here is another actual homework assignment which was presented in an intro to programming class. This program highlights more use using strings, modules, and switch statements.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Get String Input
If/Else Statements
Methods (A.K.A "Functions") - What Are They?
Switch Statements - How To Use Them
Equal - String Comparison

This program first prompts the user to enter their name. Upon receiving that information, the program saves input into a string called “firstName.” The program then asks if the user has a middle name. If they do, the user will enter a middle name. If they dont, the program proceeds to ask for a last name. Upon receiving the first, [middle], and last names, the program will append the first, [middle], and last names into a completely new string titled “fullName.” Lastly, if the users’ first, [middle], or last names are the same, the program will display that data to the screen via stdout. The program will also display to the user the number of characters their full name contains using the built in function “length.”


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
Note: The code was compiled five separate times to display the different outputs its able to produce

====== RUN 1 ======

Welcome to My Programming Notes' Java Program.

Please enter your first name: My
Do you have a middle name?(Y/N): y
Please enter your middle name: Programming
Please enter your last name: Notes

The total number of characters in your name is: 18
And your full name is My Programming Notes

====== RUN 2 ======

Welcome to My Programming Notes' Java Program.

Please enter your first name: Programming
Do you have a middle name?(Y/N): n
Please enter your last name: Notes

The total number of characters in your name is: 16
And your full name is Programming Notes

====== RUN 3 ======

Welcome to My Programming Notes' Java Program.

Please enter your first name: Notes
Do you have a middle name?(Y/N): y
Please enter your middle name: Notes
Please enter your last name: Notes

Your first and middle name are the same
Your middle and last name are the same
Your first and last name are the same

The total number of characters in your name is: 15
And your full name is Notes Notes Notes

====== RUN 4 ======

Welcome to My Programming Notes' Java Program.

Please enter your first name: My
Do you have a middle name?(Y/N): n
Please enter your last name: My

Your first and last name are the same

The total number of characters in your name is: 4
And your full name is My My

====== RUN 5 ======

Welcome to My Programming Notes' Java Program.

Please enter your first name: My
Do you have a middle name?(Y/N): z

Please press either 'Y' or 'N'
Program exiting...

Was this article helpful?
👍 YesNo

Leave a Reply