Im trying to write a method that takes two name (ie fistName and lastName) and then makes them into one name
BUT. it cant accept input that is empty and or a space
it needs to start with a Char between A-Z or a-z
the user should be able to enter the names with a combination of upper and lowercase but the name will be saved with the first letter only being in upper case (ie end output being John Doe)
WHo thought entering your name into a program is so complicated haha
any help with this would be amazing
even just string methods that i should read up on as i have no idea where to start with this
thanks in advance.
For the question above, nobody here ( true java ranchers ) is going to write the code for you... you have to help us help you... tell us till what point did you "Try"..
paste some code.... its not at all complicated if you start with the basics... and for strings you can start Here
* change your name before the moderators politely send you a private message... use a real name happy learning..
Don't try and do everything at once. Write as little code as possible (sometimes, I write ONE line of code) before you re-compile and test.
Break down the problem into tiny pieces. What is the smallest thing you can do? I'd personally start by writing a method that does nothing but print "i'm inside the method". Once that works, change it so it takes ONE name. Once you can get that to compile and run, try and PRINT the name you passed in. once that works, change it so you pass in two names, compile and test that, etc.
Once you have both names being passed in, start adding in the validation, one check at a time.
WHo thought entering your name into a program is so complicated
pretty much everyone who has ever written code
ie. input a number say 7 and it will still work.
i thought that
wouldnt let you put in a number. it would throw and exception that which then be picked up and then the user would be asked again for an input.
but this seems not to be the case?
It seems more likely, however, that you are using a Scanner object. In which case, you don't need the Exception handling, because nextLine() doesn't throw any particular Exception. Note that under certain circumstances, as described here, for example, you can get an apparently empty line from that method.
When you enter your second do loop, done is true, so your loop won't behave as you would like. You may need done = false; somewhere between the two loops.
Checking that the entries consist of letters only is reasonably straightforward with the regular expressions API, but there is a lot to learn there.
And welcome to the Ranch
"Scanner console = new Scanner(System.in);"
So the "nextLine()" method is diffrent from the other methods of that class such as "nextDouble()" and nextInt()" in that it doesnt have exceptions and just takes what ever is in the input stream and stores it.
I still dont understand how the number entered in say 9 is save in a variable that is declared as a String?
YOUR job is to write the code to validate the input. Personally, I would write another method that takes a String as an argument that returns a boolean.
fred rosenberger wrote:It's hard (for me) to see what's wrong, since I can't compile your code. I don't know what your console variable is, as it's not defined here. Can you post a Short, self contained, Correct (compileable) example? (see here for more details)
thanks for all the help so far it is really starting to come along
at the moment it is still letting me put whatever i want in ie. spaces, blanks, numbers.