• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find non-digit numbers in a string?  RSS feed

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody have an example or tip of how to search through a string (assuming it's exactly seven) and determine if there is any character other than a digit? I'm supposed to put a string in an if statement and check if the social security number has nothing but numbers, if there is a non-digit number then they must attempt to try again and enter the SSN. Here's a quick example just to get an idea...

while(!correctSSN)
{
try
{
empObject.setSSN(kboard.next());
String demo = empObject.getSSN();

if(demo.length() < 7 || demo.length() > 7)
{
throw new SSNLengthException(demo + " is an invalid input of numbers.");

}

// right here is where I need to figure out what to do
else if()
{

}

else
{
correctSSN = true;
}

}
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: A Number can be constructed from a string. The construction can fail.
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so parse would do the trick?
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Regular expressions. matches method on the String takes an RE pattern as an argument.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Avilez wrote:so parse would do the trick?

Specifically, Integer.parseInt(). you should remember to be specific and accurate when you're writing (or talking about) Java, because the compiler is NOT forgiving about things that may seem trivial to you - like spelling and capitalization. long is NOT the same thing as Long.

Another alternative would be to check that each character in the string is a digit (have a look at the Character.isDigit() method); but in this rare case it might be better to use parseInt() and only accept the value if it doesn't throw an exception.

I say 'rare' because using Exceptions for flow control is usually NOT a good idea; but in this case, it really does achieve exactly what you want.

BTW: Aren't SSNs 9 digits? Mine was.

Winston
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston

Ok cool that was actually my first plan to parse it. I have it like this: int demo2 = Integer.parseInt(demo.substring(0, 9));
I guess my last question is how do I have it enter the 2nd if else statement when there is no non-digit available?

lol and yes there is 9 numbers in a ssn. It's been a very long stressful week with school man bear with me =p
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Avilez wrote:Ok cool that was actually my first plan to parse it. I have it like this: int demo2 = Integer.parseInt(demo.substring(0, 9));
I guess my last question is how do I have it enter the 2nd if else statement when there is no non-digit available?

That's not the idea. Unfortunately, user input is very fiddly, so you need some way of making sure that the user has entered exactly what you want. Here's just one possibility:Now you have an SSN that is a proper number (as opposed to a String containing numeric characters). And to be honest, that's what I'd use for your empObject.setSSN() method, unless it's already been written for you.

Please understand that the above is only ONE way of doing it; there are many others.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!