• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception for String, not Integer

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numbers and characters are legitimate Strings.

WP
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or: William O'Sullivan ;)

WP
 
Campbell Ritchie
Sheriff
Pie
Posts: 49817
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why do you expect a NumberFormatException from a name?
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use regex to do what you need.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new, so any suggestions.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred already gave you two suggestions. The regex idea has (rightly) been thrown out. That leaves only one.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh chatacter by character. Pain in the butt, but I think I've done that before.
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic