Hi,

I'm currently writing a basic program (only been using Java for 3 weeks), and am trying to locate the "index position" of the first number in a string.

For example String = "slow by 10.40s" or "fast by 1m2.2s" and want to use a method that returns me the index position of the very first integer i encounter in the string (or even better the whole number - tried using ParseDouble but obviously get a number exception).

I've attempted to write my own "method" to loop through numbers 1-9 and return the lowest index position, but realised that if it didn't find a number( ie 3 in the above examples) the lowest index position was -1.......

I guess i could examine each character in the string individually to determine if it is an integer or not, but can't believe that the "firstInt" code isn't already in a class??

Apologies if the terminology is wrong in this post, i'm still learning about classes and methods and constructors etc......

if anyone can help point me in the right direction it would be most appreciated.

Jake

what's wrong with getting -1 if something isn't found?
It's a non-existent index for a string, therefore easy to check for.

If you get any number greater than -1 the substring (in your case a single character) you look for exists in the target string, couldn't be easier.

You do understand that the regular expression package has a standard system for doing just that?
It's already there and far more flexible than any rigid function to do a single small thing can provide.
After all, as I said, there's tons of small things someone somewhere may find useful. …

## All 6 Replies

what's wrong with getting -1 if something isn't found?
It's a non-existent index for a string, therefore easy to check for.

If you get any number greater than -1 the substring (in your case a single character) you look for exists in the target string, couldn't be easier.

If Sun were to provide every possible special case as a function of its own we'd get a complete mess of methods.
You're interested only in having a number in there, I may want that number to fall inside a given range, or maybe only want to know if a certain number occurrs more than once but less than 4 times in the input string, my colleague may want to know whether the string contains a word that in Swahili would be the phonetic spelling of a number, etc. etc.

what's wrong with getting -1 if something isn't found?
It's a non-existent index for a string, therefore easy to check for.
If you get any number greater than -1 the substring (in your case a single character) you look for exists in the target string, couldn't be easier.
If Sun were to provide every possible special case as a function of its own we'd get a complete mess of methods.
You're interested only in having a number in there, I may want that number to fall inside a given range, or maybe only want to know if a certain number occurrs more than once but less than 4 times in the input string, my colleague may want to know whether the string contains a word that in Swahili would be the phonetic spelling of a number, etc. etc.

JWenting, thanks for the reply. I'm not sure if i explained myself correctly as I don't really understand the response.

I was simply expecting that a very popular "method" would be to extract a number from a string or at least identify where the index of the first "number" within the string was. . I was suprised that the "string" class hadn't got a function like this, and was half expecting it to be in another class. I've eventually written some straightforward, but long-winded code to resolve my problem.

int bracketcheckstart = time.indexOf("(",0);
int bracketcheckend   = time.indexOf(")",0);
int timebystartpoint    = 99999;
double timebytimedouble = 0;
if (bracketcheckstart > -1) {
if ((time.indexOf("slow") > -1) ||       (time.indexOf("fast") > -1)) {
for (int num = 0; num < 10 ; num++) {
String numstring = Integer.toString(num);
int timebystart = time.indexOf(numstring,bracketcheckstart);
if
(timebystart > -1 && timebystart < timebystartpoint) {
timebystartpoint = timebystart;

}
}
String timebytime = time.substring(timebystartpoint,bracketcheckend-2);
try {
if (time.indexOf("fast") > -1) {
timebytimedouble += Double.parseDouble(timebytime);
}
else {
timebytimedouble -= Double.parseDouble(timebytime);
}

} catch (NumberFormatException nfe) {
System.out.println("TIME CHECK FAILURE : " +nfe);
timebytimedouble = 0;
}
}
else {
timebytimedouble = 0;
}

}
else {
timebytimedouble = 0;
}
return timebytimedouble;
}

Thanks again for getting back to me.

You do understand that the regular expression package has a standard system for doing just that?
It's already there and far more flexible than any rigid function to do a single small thing can provide.
After all, as I said, there's tons of small things someone somewhere may find useful. Adding them all would be a mess of monumental proportions so a generic system was provided instead using standard regular expressions.

Jwenting,

As for Regular expression package...must admit i'd never heard of this. (remember i'm only 3 weeks old ;-) and am only half way through my first java book.

I've been looking around, is this the package regex? with classes matcher and pattern?

i'll have a look at those and see if i can find some sample code somewhere on the web.

thanks for your help

jake

Yup, that's it.
If your book is old it may not even mention the package, it was introduced in 1.4 (so only several years ago).

There are a lot of resources online about regular expressions, and an excellent book from APress about using them in Java.

(remember i'm only 3 weeks old ;-)

You're the youngest programmer I know.

-Just messing with you. :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.