Two Laptop Bag*
The moose likes Beginning Java and the fly likes How do I check if a string contains a series of integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How do I check if a string contains a series of integers

Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
I have some coursework to do and I've hit a snag, the question is asking for this

If a user enters a non-existent product code, give the error message 'I don't
recognize that Product Code'. (A non-existent product code will have an alphabetic character as its first
character, followed by four digits, just like a real product code, but won't match a real product code.)


Here is a sample product code that I have in my program "L4567" I've written a condition that checks this but what I want to know is if there is a way to check if the first letter is then followed by 4 integers only.

Below is my code for the condition, I'm just checking is the first character is a letter and then after that letter anything can be entered as long as it is less than or equal to the maximum length of a product code.



Side note : I know how this would be impractical if I had more than 3 codes. The code is also part of a method

Thanks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

So it seems to me there are two issues here:

1) is it clearly not a valid code - i.e. not a letter followed by four digits. An example would be "LB5" or "82724".
2) If it is possibly a valid code, is it REALLY a valid code?

These are independent issues, and should be solved separately.

So, I would start by asking you how YOU would do #1 above. No computers. No Java. No methods. Just English, your brain, pencil, paper, and an eraser.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
fred rosenberger wrote:So it seems to me there are two issues here:

1) is it clearly not a valid code - i.e. not a letter followed by four digits. An example would be "LB5" or "82724".
2) If it is possibly a valid code, is it REALLY a valid code?

These are independent issues, and should be solved separately.

So, I would start by asking you how YOU would do #1 above. No computers. No Java. No methods. Just English, your brain, pencil, paper, and an eraser.


According to what I wrote LB5 would not be a valid code and considered in that condition so I should just change the condition to equals userCode.length == 5; I also have an else that handles anything entered.

If I were to do it with a pencil and paper it'd be very simple and easy, there isn't much thinking required.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Lee Sigauke wrote:If I were to do it with a pencil and paper it'd be very simple and easy, there isn't much thinking required.

And that is the road to failure. Try it anyways.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
Bear Bibeault wrote:
Lee Sigauke wrote:If I were to do it with a pencil and paper it'd be very simple and easy, there isn't much thinking required.

And that is the road to failure. Try it anyways.


How is it the road to failure ?

Maybe my answer lacked detail but I'd check the code against the criteria (Letter followed by 4 numbers) then check if it exists in the "database"

The only way I see it happening with java is using charAt but that would make a very lengthy condition.

I have other conditions by the way, this is just one of them, I just want to make that clear
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Because you are glossing over the details and ignoring some of the best advice you could be given.
Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
Bear Bibeault wrote:Because you are glossing over the details and ignoring some of the best advice you could be given.


I'm not ignoring the advice, I write a lot of things down before I program them but that usually depends on complexity and I assessed this particular thing as not complex.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Your own quote would indicate otherwise:
Lee Sigauke wrote:I have some coursework to do and I've hit a snag

Good luck. I'm out.
Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
Bear Bibeault wrote:Your own quote would indicate otherwise:
Lee Sigauke wrote:I have some coursework to do and I've hit a snag

Good luck. I'm out.


Okay, thank you for your time.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
I'm not ignoring the advice, I write a lot of things down before I program them but that usually depends on complexity and I assessed this particular thing as not complex.

Regardless or whether or not you think it is complex, write it out. And writing "I check if the first character is a letter and the next four are digits" isn't a description of how you would solve it. Write it down as if you were explaining it to a five year old.

Also look at the advice Fred gave. If you were shown "L56" or "L123456" you wouldn't even bother to check the letters and digits why? Scrub that, I just noticed you have already addressed this in an earlier post.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Tony Docherty wrote:
I'm not ignoring the advice, I write a lot of things down before I program them but that usually depends on complexity and I assessed this particular thing as not complex.

Regardless or whether or not you think it is complex, write it out. And writing "I check if the first character is a letter and the next four are digits" isn't a description of how you would solve it. Write it down as if you were explaining it to a five year old.

Also look at the advice Fred gave. If you were shown "L56" or "L123456" you wouldn't even bother to check the letters and digits why? Scrub that, I just noticed you have already addressed this in an earlier post.




As a side note, I have heard many reasons given for *not* trying something -- but admittedly, this is the first time the "it would be too easy to do" reason was used.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

so what ARE you trying to do? If the string isn't five characters, you know it's bad. If it is exactly five characters, you do a DB lookup.

What else is there? Why does it matter if it is a letter followed by four digits. Whether it passes that test or not, isn't a DB lookup enough?
Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
Henry Wong wrote:
Tony Docherty wrote:
I'm not ignoring the advice, I write a lot of things down before I program them but that usually depends on complexity and I assessed this particular thing as not complex.

Regardless or whether or not you think it is complex, write it out. And writing "I check if the first character is a letter and the next four are digits" isn't a description of how you would solve it. Write it down as if you were explaining it to a five year old.

Also look at the advice Fred gave. If you were shown "L56" or "L123456" you wouldn't even bother to check the letters and digits why? Scrub that, I just noticed you have already addressed this in an earlier post.




As a side note, I have heard many reasons given for *not* trying something -- but admittedly, this is the first time the "it would be too easy to do" reason was used.

Henry


I gave another description earlier and this is a simple as I think it can be

Maybe my answer lacked detail but I'd check the code against the criteria (Letter followed by 4 numbers) then check if it exists in the "database"

The only way I see it happening with java is using charAt but that would make a very lengthy condition.



fred rosenberger wrote:so what ARE you trying to do? If the string isn't five characters, you know it's bad. If it is exactly five characters, you do a DB lookup.

What else is there? Why does it matter if it is a letter followed by four digits. Whether it passes that test or not, isn't a DB lookup enough?



I'm trying to check the product code against 3 other strings which happen to be code1, code2 and code3 (this is my "db") so if the entered string starts with a letter and followed by 4 numbers then it's a valid code but it should be compared against the 3 strings I have and if it doesn't match the any of the strings then an error message is given saying the said product/product code does not exist and as I said before, this is not my only condition, it works the way it is (after some changes) but I'm just trying to find out if there is a way to do it without writing down Character.isDigit(charAt(index) for each number after the first letter.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Lee Sigauke wrote:
I'm trying to check the product code against 3 other strings which happen to be code1, code2 and code3 (this is my "db") so if the entered string starts with a letter and followed by 4 numbers then it's a valid code but it should be compared against the 3 strings I have and if it doesn't match the any of the strings then an error message is given saying the said product/product code does not exist and as I said before, this is not my only condition, it works the way it is (after some changes) but I'm just trying to find out if there is a way to do it without writing down Character.isDigit(charAt(index) for each number after the first letter.


Well, one option is to use regular expressions to check if the string is a letter followed by exactly 4 numbers -- like so...



You can also have the regex confirm that it is not the three codes too (at the same time), but that would be overkill.

Henry
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7486
    
  18

Lee Sigauke wrote:Below is my code for the condition...

Lee,

Please DontWriteLongLines (←click) inside code blocks. I've broken yours up this time.

Thanks.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

Lee Sigauke wrote:I'm trying to check the product code against 3 other strings which happen to be code1, code2 and code3 (this is my "db") so if the entered string starts with a letter and followed by 4 numbers then it's a valid code but it should be compared against the 3 strings I have and if it doesn't match the any of the strings then an error message is given saying the said product/product code does not exist and as I said before, this is not my only condition, it works the way it is (after some changes) but I'm just trying to find out if there is a way to do it without writing down Character.isDigit(charAt(index) for each number after the first letter.

This is my point:

The length/letter+4digit check appears, to me, to be completely unnecessary. The best it does is tell you that something might be good. Sure, it will let you discard a few cases, but EVERY value that passes this test still needs to be checked against your database, whatever form that database is.

So if you have to check against the DB anyway, why bother with the length/letter check? Why not just check everything against your DB? It makes your code simpler and easier to understand:


vs


Does it matter if it is formatted correctly if it doesn't exist in the DB?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7486
    
  18

Lee Sigauke wrote:The only way I see it happening with java is using charAt but that would make a very lengthy condition.

Not really. Since your pattern is a letter followed by four numbers, there are several things you can use to minimize your checking:
1. String.length().
2. Character.isLetter(char).
3. Character.isDigit(char).
and you can use a loop to check the last 4.

However, for stuff like this, Henry's suggestion of a regex is probably the most generic.

Winston
Lee Sigauke
Ranch Hand

Joined: May 16, 2012
Posts: 30
Regex works perfectly; I can now add it to my list of learned things.

I'll keep my codes shorter in future

Thank you very much for your assistance, I appreciate it
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

it's an old joke, but still rings true:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7486
    
  18

Lee Sigauke wrote:Regex works perfectly; I can now add it to my list of learned things.

Actually, I suspect not. I've been using them for about 20 years and I still discover stuff I didn't know about them.

And, powerful as they are, there is a price in terms of efficiency (although this is usually small enough not to worry about) and readability (just check out this monster if you don't believe me).

It's also worth finding out when they shouldn't be used:
As someone wise once wrote:Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I check if a string contains a series of integers
 
Similar Threads
Remove non-letter characters from String
Big Smokes - Who cares about manufacturer stock levels?
Checking whether a string is a substring of another string without using indexOf method
how to compare elements of List
Exception problem.