• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring question  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm doing a problem where I construct an ID out of someone's name and their street address, using the first letter of their first and last name and the numbers of their street to create it.
Somewhat shockingly, I've done it without asking for any tips. I'm struggling with one part though. When you look at the address.substring I don't know how to get it to stop at
just the numbers. For example (not that it's practical), if someone were to put their address as "9483738383484 South Elm Street" it would cut off at 7 due to the limit of 5 below. But, likewise, if
someone were to put "11 South Elm" it would come back with 11 So, which I don't want. Is there a way to alter it so it knows to stop at just the end of the first character or digits?


 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. If the String length <= 5 don't get the substring (beware of IndexOutOfBoundsException).
2. Replace anything that is not a number String#replaceAll with an empty String ""
Regex for matching non-digits is \D. Make sure you escape \ properly.

A thing to consider. What if there are no digits in data typed by user? What if the street number is typed AFTER the street name?
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mind explaining how to use replaceAll? I looked at the link but have never seen or used it before. If you had an example of it being used in a program I'd appreciate it.

Actually, more specifically, can you explain how to use it with joptionpane? I understand if I know the input already but not how to use it prior.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:Is there a way to alter it so it knows to stop at just the end of the first character or digits?

Yes, but even simpler might be to split up the address line into "words" and try an match one that contains only numeric digits (maybe suffixed with a letter to allow for things like "221B Baker St".

Winston
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In real life you'd have to deal with people who put nothing in there, or "None of your dam business" and so on. But for a programming exercise I would just split the string into words (using spaces as the delimiter) and use the first word (if there is one).

And when I said "split", I meant... check out the API documentation for String and see if there's something about "split" in it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:In real life you'd have to deal...

@Ryan: And also different ways of writing addresses. In Belgium (where I live - and a lot of Europe) the number goes at the end:
Berlinerstrasse 68
...

which is why I suggested that you look for a "word" with numbers in it.

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