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
Robin has made several good points, but i'll add this:
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
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
What class is console? Is it a Console? In which case: beware: if you start your application with javaw rather than the java tool, your Console will be null and you won't be able to get any input from it.
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.
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)
Joined: May 03, 2011
Yes it is from the Scanner class
"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?
Because on the command line, '9' is a character, just like 'a', '+' or 'Q'. You could also input "Fred is 17 years old" and the Scanner object reads the entire thing. It doesn't know what is or isn't valid based on your rules, it just reads whatever it is given.
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.
Joined: May 03, 2011
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.