Question
JAVA: The do while loop should not be your first choice when writing an iterative solution. As a general rule, if you can solve the
JAVA: The do while loop should not be your first choice when writing an iterative solution. As a general rule, if you can solve the problem reasonably with a while loop then you should do that. With that being said, there is no reason why we can't practice it a little bit.
Ask the user to input a list of their favorite names. We will consider only one word names for this exercise. The user should keep inputting until "x' is entered as the name. Use a do-while loop to get all of the names. When the loop exits, output a string that contains the first initial of each name comma separated.
Hint:
You actually don't need to store each name to solve this. They enter a list but you should only concerned with the first initial.
Refresher
You can get a single character from a string by using the at function. For instance, give the the string
String str = "hello";
I can get the first character like this:
char ch = str.charAt(0); // The character h is assigned to ch
I can also add characters to the end of a string by using the += operator
str+='.';
The string str now contains "hello."
Example run:
Enter a name 'X' to exit
Joe Glenn Hank Bob X
The first letters are: J,G,H,B
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started