• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String input with exceptions

 
Richard Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hay Guys first post. Ever. so be gentle

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.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi "JavaMan DarkSide" ( used camelcase )... Welcome to the JavaRanch....

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..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Richard Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about the name.

anyway.

here is what i have so far.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And does that work? does it compile? What does it do that you don't expect, or not do that you do expect?


Note: In my first post, I was assuming you were going to pass strings into your method, and not get them as user input....but the general advice still stands.
 
Richard Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles but then accepts any input that you give it.

ie. input a number say 7 and it will still work.

i thought that

console.nextLine()

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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

And welcome to the Ranch
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Richard Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Richard Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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






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.
??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Young wrote:at the moment it is still letting me put whatever i want in ie. spaces, blanks, numbers.
??

I guess that's the question - why do you think the Scanner class would limit the input to only alphabetic characters?
 
Jared Malcolm
Ranch Hand
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you ever figure this out?

The other thing I noticed is that your second block of code will NEVER run twice and can't fail... Leave it up to you to see why....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic