Two name validation

Hi everyone!

I need a tip/help with validation in my program.
I’m creating a program where I will ask the user’s first and last name, at the same readline
For this, I’ve created a string named “name” where I need to check if the first name starts with an upper case and the same for the second name.

I’ve been searching everywhere but I’ve got nothing.

Can someone help me with a tip for this one?

I’m sorry if this is a basic thing, I’m in the beginning.

1 Like

name.split(" ").all { it[0].isUpperCase() }
Basically what this code does is that it splits the input string into an array of strings. That array basically has every word in the input string that had spaces between them, so in your case it’ll include the first and last names. Then, .all is a function that returns true if every single element in the array follows a specified condition, and false otherwise. The condition that you need in this case is to check that the first character in the string (index 0) is uppercase, which is exactly what this code does. You can then include this in a condition like this:

if (name.split(" ").all { it[0].isUpperCase() }) {
    println("You've used correct punctuation!!")
} else {
    println("You must be getting incredible grades in English...")
1 Like

Another question “please help me doing my homework”… :slightly_frowning_face:

1 Like

Yeah, I generally prefer giving only tips for those kind of questions eg. "Take a look at String.split and Char.isUpperCase". Even if it’s not a homework assignment figuring out a solution helps you learn far more than just copying one, especially in the early stages of learning programming.
After OP posts their result here we can still show them more optimal solutions. For example I wouldn’t expect a beginner to think of the all function on their own insetad I would expect them to use a for loop or mabye even just check the first and second element in the split string.


If you don’t know how to do it and you’ve been searching all day long, you have to ask
Wherever if it is for homework or for whatever it is.
Have a good day! :slightly_smiling_face:

1 Like

Thanks all for the help

1 Like

I don’t mind that you asked (can’t speak for anyone else). I more dislike the fact that you got a complete answer and not just a hint to solve it for yourself. In the case I only give hints I normally add a sentence like “If you have any more questions or get stuck feel free to ask more” or something like that.

I know the feeling but it’s hard for us to see that. Depending on your questions you could add links to what your searched for already but sometimes it’s just incredibly hard to know what to search for especially if you are new to a topic.
Generally your approach should be something like

  1. spend some time trying to figure it out yourself.
  2. if you can’t, try to split up the problem into smaller problems
    • in in this case that would be something like 1. split name into firstname - lastname and 2. verify that both start with uppercase
  3. look for solutions to your smaller problems (the more specifc/smaller a problem is the easier it is to search for)
  4. Ask for help (and if and if possible link to possible solutions for you problem parts)

That has a few advantages. Firstly you often figure it out yourself by setp 3 and secondly people on the internet are much more likely to help you if they see that you put in a decent amount of effort first. I think I can speak for everyone answering here that we do like to help people if we can. What I (and and porbably most others here) don’t like is writing their programs for them :wink:

Also I’m not saying that you didn’t follow these steps (or something similar). Just wanted to leave this as possible advice for the future (for you and everyone else who might stumble on this). Learning how to search for programming related issues and asking questions is one important part about learning to program (even though it’s not something that is normally taught).


I should’ve probably chimed in on this conversation earlier, but personally I do perfectly see your point. Usually I refrain from giving fully-blown solutions unless it’s a very niche issue. However, in this case I felt like because the solution included fundamental properties of the language that OP seemed to not know about them that I should probably run them through each part of it step by step to ensure that it clicks in their mind. The solution also seemed to be like quite simple once it is written out, and so giving a couple of hints like the function names would’ve either been too easy to figure out (and so it would make sense to j give the actual code) or too hard to figure out if you aren’t used to the idea of hopping between types to solve a problem (which then could be practically demonstrated with an example like that because it shows how you have to like make the code flow between each part and the next). I also included the function all because personally I believe that knowing about functions like these early on makes programming sounds a lot more natural than going through the whole “well you gotta make a for loop and have a condition that is set to true at the start and gets set to false if any of the items don’t follow the blablabla” bazzaz because it sort of overcomplicates what should be a rather simple task). I do definitely agree though that in most situations giving out hints is probably the best option for most questions.

1 Like