Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Referencing more than one index in an if statement?  RSS feed

 
Ranch Hand
Posts: 64
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trying to make characters in my input numbers only from index 4 to 20

is there a way i can do it here...or do i have to have an if for every index?

tried 1,2,3 didnt like it
tried 4-20 gives me an error

 
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean if (Character.isLetter(text.charAt(123)) && Character.isLetter(text.charAt(321))) ... ?
No, you can't. Yes, you can, but you are getting into the realms of illegible code here.

Consider a regular expression, as I said earlier, or:-
Iterate the text character by character. Count until you stop having digits. Then count how many letters you have. Then count how many digits you have following.  Do some arithmetic.
But I still think the regular expression will work better.
 
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I can't understand what you're asking. And what is the error you're getting? A Stack Trace would help a lot.
 
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:trying to make characters in my input numbers only from index 4 to 20


I suspect you're trying to do more than just that so, to ask Campbell's more succinctly:
What are you trying to do?

Clearly you're trying to work out what "type" of value you've got, but is it just Car Registrations, or is there more to it?
And what do you want to do when it isn't a Car Reg?

It's difficult to provide a solution without a complete description of the problem.

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

wayne brandon wrote:trying to make characters in my input numbers only from index 4 to 20

is there a way i can do it here...or do i have to have an if for every index?

tried 1,2,3 didnt like it
tried 4-20 gives me an error


Are you trying to filter your input allowing only characters from the range of indexes of 4 to 20? Then what is the index mean in this context? Is it a character code?
 
Tyoma Sakurakoji
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:trying to make characters in my input numbers only from index 4 to 20

is there a way i can do it here...or do i have to have an if for every index?

tried 1,2,3 didnt like it
tried 4-20 gives me an error


Or maybe you mean that letters letters containing numbers in your input string should be allowed only in postitions starting from 4 to 20? In that case you can write regular expresion checking is it true:
Means input string must start from 4 non digit letters, then having 16 digit letters and then any number of non digit letters...
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestion, but I think that regex is incorrect. Please don't post any more; OP will learn much better if he works it out for himself.
 
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you share the format of how a registration number looks like (just an example) ? there are lot of combinations and numbers depending on country, which type do you want to support in your code ?
Also note that "charAt" method will throw a java.lang.StringIndexOutOfBoundsException if you are trying to access an invalid location. e.g.

 
Winston Gutkowski
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Can you share the format of how a registration number looks like (just an example) ? there are lot of combinations and numbers depending on country


Which is why regexes are almost never the solution to a pattern-matching problem, unless the pattern P is guaranteed to satisfy ALL cases and NO others.
Otherwise, it's just a filter, and all you can expect is that P is "probably" a URL/e-mail address/Car Registration, etc.

That doesn't mean they're not useful; just don't think of them as a "solution".

The last "solution" I saw for an e-mail address on the regex site was about 500 characters long, and I didn't understand it (and I've been a Unix programmer for 30 years). It's probably correct - those guys are pretty good - but do you really want to trust your code to something that 0.0001% of the population can follow, let alone debug?

Winston
 
salvin francis
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't suggest regex .... Yet  
 
Tim Holloway
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:The last "solution" I saw for an e-mail address on the regex site was about 500 characters long.



Really? Was this a simple Internet email address regex, or one of the ones that had to deal with gnarly stuff like BitNet and other predecessor mailing environments like "bang" notation?
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must have been simple if it fitted into 500 characters.
I thought this example was simple enough that you could get a regex to fit.
 
Winston Gutkowski
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Really? Was this a simple Internet email address regex, or one of the ones that had to deal with gnarly stuff like BitNet and other predecessor mailing environments like "bang" notation?


No idea; and it may well be old.
Unless you missed, I've been "offline" for a while. :-)
And I still say it's the WRONG way to solve the "problem".

Winston
 
wayne brandon
Ranch Hand
Posts: 64
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

thanks for all the interest

i need the user to enter a registration number that matches this criteria

12TS (and then any amount of numbers following the letters)

so if you put in
as122424

it just prompts again for them to reenter it

so will 1223ac1222 re prompt

it just has to follow two numbers, two letters and as many numbers as they want

campbell i looked at the tutorial on regex you sent me...im a real newb to all this...it was very daunting...but i will look at it again if you think its the best way to do this,for now and the future

thanks all
 
Tim Holloway
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a regex problem to me.

But regexes are good for lexical parsing and validation. Once you're past that, you then often have to deal with semantic validation, which I suspect is where Winston's reservations lie. Though he might be one of the vast number of people who've been scarred by the arcane intricacies of the regex "language".

Given the current problem statement I think a regex will do fine here. Start adding additional requirements and I may start suggesting alternative strategies.

Incidentally, Perl and Python are my primary languages to write "quick-and-dirty" utility programs in. If the input data is more suitable for regexes, I generally use Perl. Otherwise, I lean towards Python. Mostly because regexes are inherent to Perl but require extra effort in Python. If I need special function packages such as LDAP services, though, I will bite the bullet and use Python regexes. CPAN has given me a fair amount of grief, but Python package libraries have been less problematic.
 
Winston Gutkowski
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Sounds like a regex problem to me.


Totally agree: Single field; simple pattern - does it match or not? Made for regex.

@Wayne: Hope you're not looking for us to give you the pattern.
Do a bit of research about regexes, and see if you can come up with one of your own.
If you're a bit out we'll be glad to help, but we want you to do some work first. :-)

Winston
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . 12TS (and then any amount of numbers following the letters) . . .

Two numbers, two letters, followed by any number of numbers. Sounds easy enough.
 
salvin francis
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regex would be a simple solution, agreed. But even if OP does not know regex, isn't it simple to simply loop over the characters ? :

 
Tim Holloway
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Regex would be a simple solution, agreed. But even if OP does not know regex, isn't it simple to simply loop over the characters ? :



A Regex is basically a program to do just that. So same thing, but collapsed.

Actually regex processing is typically done by a Finite State Machine, and for efficiency reasons Java compiles it down into some sort of regex bytecode sequence (not to be confused with JVM bytecodes). The regex match() method then runs the FSM against the input, using the compiled pattern to drive it. I haven't looked, but likely its mainly a big switch statement augmented by actions that use Character is... methods, character table lookups, range and constant tests and the like. You can, however, get the same effect with other automatons such as a Threaded Language Interpreter.
 
salvin francis
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:... do i have to have an if for every index?...


My loop was based on this statement by OP
 
wayne brandon
Ranch Hand
Posts: 64
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all thanks for the help....greatly appreciated this side!!!

Hey Salvan are you from India?

was there over Christmas...was amazing!!!
 
Saloon Keeper
Posts: 5456
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

wayne brandon wrote:. . . 12TS (and then any amount of numbers following the letters) . . .

Two numbers, two letters, followed by any number of numbers. Sounds easy enough.

Sounds like it's not just "two numbers, two letters", but, specifically the characters "12TS".
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case, you can match 12TS with the regex 12TS, would you believe. That won't match 12ts however. That is half of it.
 
Tim Holloway
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:In which case, you can match 12TS with the regex 12TS, would you believe. That won't match 12ts however. That is half of it.



And there you touch on one of my pet peeves. Overly-stupid data entry systems that can't handle spaces, dashes, dots, or other readability separators, forcing the user to type in telephone numbers, credit card numbers, and so forth as one big long error-prone number.

In the case of a VIN, the normalised form of the VIN is to use upper-case letters, but since it's more natural to type in data without bouncing off the SHIFT key, the first thing that I'd usually do would be to use the String upperCase method to fold all the letters to upper-case (normal) form. You can easily code a regex to deal with mixed case, but if the ultimate target is going to be a database, might as well start off on the right foot and save the extra complexity.

For things like credit card numbers, one of the String replacement methods can get rid of the visual aid characters, since they're for people's benefit and generally not wanted by processing logic or data stored. The String replaceAll method even allows using a regex as its argument if you need something more ambitious.
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . And there you touch on one of my pet peeves. . . . .

And I think you are right to be annoyed about what is simply incompetent programming. If I want to enter a phone number for Bedford, for example, it should read (01234)567890, and many supposedly well‑designed sites will complain about the (). If you look on the British Telecom website, the phone number is clearly shown with () and maybe with spaces too: (01234) 567 890.
 
Saloon Keeper
Posts: 2321
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:And there you touch on one of my pet peeves. Overly-stupid data entry systems that can't handle spaces, dashes, dots, or other readability separators, forcing the user to type in telephone numbers, credit card numbers, and so forth as one big long error-prone number.


I see this the other-way around with online shopping.  In Canada, a postal code is a six-character string consisting of alternating letters an numbers, with a space separating 3rd and 4th characters - for example: V6A 1B2.  Many sites insist on that space character be entered.

It wouldn't surprise me if after validation, some of those same sites then striped-out the space and stored the postal code in a more compact form.
 
Tim Holloway
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's actually quite decent of them. When you read a Canadian or UK postcode aloud, you basically read it as 2 "words", so allowing/requiring a space in the middle is only reasonable. Although since a lot of apps serving Canadian customers also server US customers who have 11-digit ZIP codes, it's probably not uncommon for the space to get stored anyway, since in a fixed-length field, you've already wasted the space.

 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . a Canadian or UK postcode aloud, you basically read it as 2 "words" . . .

Don't know about Canada, but the original intention in UK was that the first part of the postal code sorted mail to a town (or part of town) and the second part was used on arrival in that town to sort mail by street. That is why they are in two parts.
 
Ranch Hand
Posts: 490
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As suggested by Campbell Richie I am going to play with javap, or better as I use Kotlin, I use the intelliJ function that shows bytecode directy, this opened me a word
I am going to study this. We live in an era of frameworks that do a lot of magic, I really want to know  this kind of  assembly(?) thing https://www.ibm.com/developerworks/library/it-haggar_bytecode/index.html

So far so good, I need to study it a bit before to express any opinion, thank you very much, hopefully will come back in this thread, nice to see all your reactions. Even if Campbell Richie noticed that I am doing an high level language, is beautiful because
1) is kinda of sexy, fascinating experience, feel a kind of god
2) Who was a child that was breaking the toys to see what was inside, want to do it for curiosity
3) You become a better programmer. Do not agree? As never ending junior, I spent a lot of time to try to understand frameworks that were doing a lot of magic, making really difficult to debug third party libraries, to understand why something is happening, once one understand how the machine process things, it looks much easier doing some coding

Thanks
 
Ron McLeod
Saloon Keeper
Posts: 2321
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know about Canada, but the original intention in UK was that the first part of the postal code sorted mail to a town (or part of town) and the second part was used on arrival in that town to sort mail by street. That is why they are in two parts.


Same idea -- the fist part is the sorting area and the second part is the specific delivery area.

Trivia fact: The Canadian Post Office assigned Santa Claus a special postal code for his location at the North Pole: H0H 0H0
 
Tim Holloway
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah, forgot the North Pole code.

ZIP code comes from the US Postal Service Zone Improvement Plan. When I was a lad, my mailing address was in Zone 5. ZIP changed that to 32205.

The first 3 digits of the ZIP are a geographic designator, running from Northeast (100110 is in NYC) down East (anything starting with a 3 is the Southeast corner of the country), runs up and down and around vaguely following no obvious political or geographic boundaries I know of. And if you want street/building precision, you need ZIP+4. People don't like that one, since we're paranoid about privacy and its too many extra digits.

A lot of time post office boxes will have their +4 parts simply echo the box ID. Some organizations have multiple internal ZIP codes. For example, the Internal Revenue processing centers have at least 2. One to be used if you're due a refund, one for if you have to pay more.

One of the Caribbean countries doesn't have postcodes at all. I think it may be Barbados, but I forget.
 
Sheriff
Posts: 24074
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:I see this the other-way around with online shopping.  In Canada, a postal code is a six-character string consisting of alternating letters an numbers, with a space separating 3rd and 4th characters - for example: V6A 1B2.  Many sites insist on that space character be entered.


And many others require that the space character NOT be entered.   And you can't guess in advance whether they want the space character or not, or whether they don't care.   Couldn't they just hire a   programmer?
 
Winston Gutkowski
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:And there you touch on one of my pet peeves. Overly-stupid data entry systems that can't handle spaces, dashes, dots, or other readability separators, forcing the user to type in telephone numbers, credit card numbers, and so forth as one big long error-prone number.


Couldn't agree with you more. Even "dumb" phones should be able to handle a space bar for clarity; although changing the tone system to include a "space" or "ignore" tone might be a bit harder, and I do love my land line. :-)

Winston
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!