Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes String input with exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String input with exceptions" Watch "String input with exceptions" New topic
Author

String input with exceptions

Richard Young
Greenhorn

Joined: May 03, 2011
Posts: 5
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

Joined: Sep 10, 2008
Posts: 270

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..


Time is what we want the most, but what we use the worst. -- William Penn
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

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
Richard Young
Greenhorn

Joined: May 03, 2011
Posts: 5
sorry about the name.

anyway.

here is what i have so far.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

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

Joined: May 03, 2011
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38087
    
  22
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

Joined: Oct 02, 2003
Posts: 11172
    
  16

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

Joined: May 03, 2011
Posts: 5
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

Joined: Oct 02, 2003
Posts: 11172
    
  16

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

Joined: May 03, 2011
Posts: 5
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

Joined: Oct 02, 2003
Posts: 11172
    
  16

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

Joined: May 02, 2011
Posts: 54

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....


SCJA 6 (Studying for SCJP 6)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String input with exceptions
 
Similar Threads
matching a string in a line
No scrolling in text input after uppercase ?
regex for nameFields: first & last names tested separately
lower case & upper case
quote