• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find non-digit numbers in a string?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}

}
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so parse would do the trick?
 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use Regular expressions. matches method on the String takes an RE pattern as an argument.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic