Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to check string if non-digit?

 
Abdallah Taha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning Abdallah!

Can you use Java's standard methods in your solution?

 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the standard Java® naming conventions. Use so‑called  camelCase not underscores_between_words. Use underscores in C, Eiffel, etc.
It is inappropriate to call the parameter ssn in line 19 because there is no relationship between SSNs and whatever text is passed to that method. In fact the type of text has no significance to that method; you can pass any String at all. I trust you realise that there is a corner case where your method's logic will return what you don't expect. Call that method containsOnlyDigits() or similar, not testingXXX() please.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AT, please work out why you will not accurately pick up 9666 with your technique. Also find out which techniques will and which won't pick up numbers starting 0.
 
Abdallah Taha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:AT, please work out why you will not accurately pick up 9666 with your technique. Also find out which techniques will and which won't pick up numbers starting 0.


Thank you. I fixed but still a little bit wrong when do these questions 1. None of the 3 groups of digits can be all 0’s.
2. The first group of digits cannot be 666.
3. The first group of digits cannot start with a 9.
the output show :
Enter Social Security Number:000-00-0000
000-00-0000 is invalid
000-00-0000 is valid.

here my program edited :
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abdallah Taha wrote:. . .
2. The first group of digits cannot be 666.
3. The first group of digits cannot start with a 9.
. . .

There is too much in your program to go through the whole of it all at once. But look at lines 46‑49 and work out what will happen if you enter 678‑12‑3456.
Your logic looks very complicated to me; anything that complicated is going to be error‑prone. There must be a simpler way to do it.
 
Knute Snortum
Sheriff
Posts: 6268
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some other things to think about.

* Your comment says you are expecting the SSN in the form nnn-nn-nnnn, but then you don't prompt for that.
* You don't check whether in input is 11 in length.
* firstgroup4 and secondgroup3 should only ever be '-'.
* Checking for Character.isLetter() is not the same as checking for a non-digit character.
* The values in the substring() method at lines 71 through 81 don't seem right. See the documentation on substring.
 
salvin francis
Bartender
Posts: 2371
102
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this would be a shorter code with a Regex expression. You can use regex to extract the individual groups of numbers from the input and then do your code logic of checking against 666 and 9, etc...

if you're not familiar with regex, you can read about it here : https://docs.oracle.com/javase/tutorial/essential/regex/
Ideally, it would be possible to do the complete validation of the 666 and 9 too using regex, but I'd not suggest that.
 
Jan Daermann
Ranch Hand
Posts: 30
1
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 on the suggestion to use regex. You might find some valuable input if you google 'validating SSN with regular expressions'.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!