• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java programming theory/help

Koing

Elite Member <br> Super Moderator<br> Health and F
I read this guy wants this done. This interested me and I thought of how I'd go about doing it.

This is what he wants done:

1. Read In from Keyboard a person's first, middle and last name, all in a single input with spaces. (e.g, John Fitzgerald Kennedy)
2. Print out
a. Last name, First and middle (eg, Kennedy, John Fitzgerald)
b. Persons intials in order (JFK)
c. number of leters in each of there names
d. Name that contains the most leters, if a tie is formed then print "tie"

Now I don't actually want you guys to write it out for him. I want to know if my idea of doing it was right or wrong.

Well here is my idea of how I'd do it:

1. Read In from Keyboard a person's first, middle and last name, all in a single input with spaces. (e.g, John Fitzgerald Kennedy)

Store as a strong and with the gaps as they are important to help out the rest

2. Print out
a. Last name, First and middle (eg, Kennedy, John Fitzgerald)

Get the code to read past the 2nd space and to print that out first and print the string as normal looping back to print the first and middle.

b. Persons intials in order (JFK)

Print the first letter after the space apart from the first name which just prints the first letter of the string.

c. number of letters in each of there names

Count the length of the string until it reach's a space then restart the count storing the previous number in a variable to compare later.

d. Name that contains the most leters, if a tie is formed then print "tie"

Print the variable that has stored the number of letters in. Compare them and print the biggest one. If tie print tie.

Just trying to improve my programming skills and thinking in programming.

Thanks.

Koing
 
While you would start by storing the user input as a string, I would then split it up into the three parts you expect and treat each name separately. It makes the subsequent logic easier. If you have first, middle and last names as separate variables, then each of the subsequent tasks is much easier.
 
in cases like this i would recommend using a StringTokenizer, which you can set to break up the string by the spaces... save each parsed string into respective variables. then perform whatever operations for output
 
Originally posted by: xUCIxDaiSHi
in cases like this i would recommend using a StringTokenizer, which you can set to break up the string by the spaces... save each parsed string into respective variables. then perform whatever operations for output

Agreed. StringTokenizer is the best way to do it.
 
Originally posted by: ClueLis
Wait... you want our help so that he can pay you to do his homework? 😕

No man. I would not go through this trouble for $5! I'm just wondering how I'd do it if I to do something like it. Just want to improve my java/ programming thinking.

Koing

 
Make a person object with 3 fields. firstName, middleName, lastName

Tokenize the original input and fill in the 3 fields.

Then make a method for each of the things you need done....

 
Back
Top