aspose file tools*
The moose likes Beginning Java and the fly likes Exception for String, not Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception for String, not Integer" Watch "Exception for String, not Integer" New topic
Author

Exception for String, not Integer

Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
I want to throw an exception when the user enters any series of integers. The only values they should be able store are letters, as in their first and last name.



I know how to do the opposite:


In other words, the bottom code snippet works, the top one does nothing. It is a String after all.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Numbers and characters are legitimate Strings.

WP
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

You need to define what EXACTLY is allowed and what isn't. What if the user enters "Fred Rosenberger the 3rd"? Or "Fred Rosenberger (III)".

If you only want to allow letters, then technically "fred rosenberger" is not valid, since it contains a space.

Until you can definitively explain what is and isn't allowed, there is no point in trying to write code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Or: William O'Sullivan ;)

WP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
And why do you expect a NumberFormatException from a name?
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
All very good points, just make sure to present such ideas with a little more tact. I may be new to programming, but not to logic and sound reasoning. That being said, I would like the user to only be able to enter a first name, followed by white space, and then a last name. Nothing else. I.e John Doe.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Two ways that immediately come to mind are going though it character by character and validating as you go, or using a regular expression.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

You can use regex to do what you need.
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
Ok, I looked at regex but wasn't sure. It's rather complex for my level. Any good tutorial sights for that?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

There is a famous quote:
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

If you are new to programming, this may not be the way for you to go.
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
I am new, so any suggestions.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

Fred already gave you two suggestions. The regex idea has (rightly) been thrown out. That leaves only one.
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
The site had not updated when I posted. I didn't see the additional info. Thanks Fred. I'll try that
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
Ahh chatacter by character. Pain in the butt, but I think I've done that before.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Ian Burres wrote:Ahh chatacter by character. Pain in the butt, but I think I've done that before.


How is that a pain in the butt? You just have to code the check for one of the input characters. And code the loop around it. It is the computer that is doing the same redundant task character by character...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
Pain in the butt because now I have to review the process needed to get it to work. I notice programmers treat their craft as an esoteric artform. I'm still learning my friend.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

Honestly, if you've worked with arrays before, it shouldn't be that hard.

The reason I say arrays, is because there is a method in the String class that can convert strings to a char array.
Then there is a method in the Character class that can test the characters to see if they are letters.

There might be other ways, but that's the way I would do it.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Ian Burres wrote:Pain in the butt because now I have to review the process needed to get it to work. I notice programmers treat their craft as an esoteric artform. I'm still learning my friend.


Perhaps you need to meet more programmers. Most of the ones that I know really enjoy their craft -- the problem solving, the brainstorming with colleagues, the constant learning, and yes, even teaching (and mentoring).

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Ian Burres wrote:Ahh chatacter by character. Pain in the butt, but I think I've done that before.

I've never dug into the logic behind a regex, but I would think they would be iterating through the string character by character as well. I don't think there is any way around it. It's simply a question of whether you "roll your own", or use something pre-built and deal with the overhead of learning that.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Regex is overkill for this problem, even if you weren't a beginner. The real power of a regular expression is that you can use it to search for multiple things in relation to each other. Here, we simply have a rule about what characters are allowed, yes?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

The beginning of a string,
followed by a series of one or more alphabetic characters
followed by exactly one space
followed by a series of one or more alphabetic characters
followed by the end of the string

That sounds exactly like a regex to me...

not a very COMPLICATED regex, granted.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
fred rosenberger wrote:The beginning of a string,
followed by a series of one or more alphabetic characters
followed by exactly one space
followed by a series of one or more alphabetic characters
followed by the end of the string

That sounds exactly like a regex to me...

not a very COMPLICATED regex, granted.


Fair enough, I concede the point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception for String, not Integer