• 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 check string if non-digit?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning Abdallah!

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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 37
1
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic