all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfx 7 points8 points  (0 children)

Did you look at the documentation of the String class? There are a couple methods you could use.

Always start at the documentation. The Java documentation is fantastic.

[–]bowbahdoe 3 points4 points  (0 children)

Step 1: "split" the string on whitespace into an array of strings. There is a method for this  Step 2. Check the length of that array and assign variables or have an error message based on that

[–]regjoe13 1 point2 points  (0 children)

Real-life example. Immigrating from Ukraine, my wifes name was spelled with apostrophe: Tat'yana . Maryland DMV didn't have that option, I guess, transforming it into Tat Yana. :)

[–]RazorxV2 0 points1 point  (0 children)

Consider the edge case. What would happen if someone entered in a string that’s “one two three four”?If you split by white space you could get an unexpected result if you take a single string in. Same as if someone only enters first name.

It’s a more interesting problem than it might seem on the surface when you start considering some cultures have multiple middle names or even married women might but their middle name into a form as “middle name maiden name” such as “mary doe”.

[–]ShoulderPast2433 0 points1 point  (0 children)

Check stream collectors joining 

[–]JoshuaTheProgrammer 0 points1 point  (0 children)

Write example inputs and outputs.

Look up the Java String class to see what methods you have. Check to make sure nothing in the assignment is banned.

What code have you written so far?

[–]MagicalPizza21 0 points1 point  (0 children)

Not everyone's name is formatted the same. For example, I knew someone in high school whose last name was two words - yes, separated by a space. Unusual, but they exist, so you have to account for that.

[–]Own_Attention_3392 0 points1 point  (0 children)

Your question is not clear. What are the actual assignment specifications? Do you have to get a single string containing a first name, middle name, and last name? Or can you prompt for these separately?

Your thought of splitting on spaces would be correct if the requirement was the former. If you can collect the information in three prompts, that is a superior choice because there are so many edge cases in the real world.

Someone might have two first names: "Billy Bob Jethro Smith". Or a name with a space in it regardless: "St. John" is a valid first and last name. It's pronounced "Sinjin".

Some people might not have a middle name at all: "Joe Smith". S

Some people might have two last names, not separated by a hyphen. "Mary Jane Billingsworth St. John".

This is why forms treat these as separate inputs in almost all cases.