aspose file tools*
The moose likes Beginning Java and the fly likes validate incremental numbers in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "validate incremental numbers in a string" Watch "validate incremental numbers in a string" New topic
Author

validate incremental numbers in a string

Anna Jean
Greenhorn

Joined: Dec 23, 2003
Posts: 8
Hello,
I am trying to determin if the numeric numbers in a string are incremental. I have written the following code(and this is assuming that s is not null and is numeric), but there is something that is still not working. Any feedback or suggestions on a better way to accomplish this would be greatly appreciated.
public boolean validateIncrementalNum(String s){
int iNumCount = 0;
for(int i = 0; i<s.length(); ++i) {
if(s.charAt(i) == s.charAt(+i))
{
iNumCount++;
}else {
break;
}
} //end for loop
if (iNumCount == s.length())
{
return false;
}
}
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Well, the problem primarily seems to be in this line:

This is saying: If the char at i is equal to the char at positive i then...
Change that second .charAt call to s.charAt(i+1)
Also, you don't want to check and see if they're equal, you want to check to see if one is one more than the other.
So, you should change your expression to something like:

Now, because of the first change, what happend if i == s.length()? Well, you can't take the charAt s.length(), because that is not a valid index. Therefore, you need to limit your looping:

This will allow you to check all pairs of consecutive numbers.
Now, since you are only performing s.length() - 1 checks, iNumCount will never be equal to s.length(). So, you should change you success test:

Now, do you want to return false if the numebrs are sequential? I don't quite think so. try:

Also, what if iNumCount does not equal s.length() -1? You are missing a return statement (the compiler will actually tell you about that one...).

Finally, Java is an excellent language for in-line debugging. Generally, if you can't figure out what is going on in a program, some System.out.println() statements will nicely do the trick. (You could use a fancy debugger, but Java tends itself more toward the in-line debugging...)
While we here at JavaRanch enjoy helping people out, it behooves you to know how to do this in-line debugging. Below is your method with some debugging statements thrown in so that I could see what was going on:

PS: please use [CODE] and [/CODE] tags when posting code. It helps us read it better...


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Anna Jean
Greenhorn

Joined: Dec 23, 2003
Posts: 8
Hi Joel,
It all makes much more sense now. In fact, the way you explained it, it seems so simple I am wondering why I could not fiure it out. Anyway, I truly appreciate your time and great explanation. It has helped significantly. I will also be sure to use the [code] tags on future posts.
Thank you.
 
 
subject: validate incremental numbers in a string