Subject: Parse integer out of string 2005-07-07 - By Frans Verhoef
Back Well, I just tested it, and this ...
int yourInt; String yourString = "A string that contains number 30 that needs to be parsed out"; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(yourString); if(m.find()){ yourInt = Integer.parseInt(yourString.substring(m.start(),m.end())); System.out.println("yourInt="+yourInt); }else{ System.out.println("hey, doesn't work!"); // take your action in case no integer found in yourString }
... works perfectly fine. Probably something else that does not work in your code.
Cheers Frans
On Fri, 8 Jul 2005 11:51:05 +1000, Taco wrote: >?I realize that. My point is it doesn't work.. > >?See example that I included, and you will see it should have worked >?but did not, hence the error. > >?-- --Original Message-- -- >?From: A mailing list for Java(tm) 2 Platform, Enterprise Edition >?[mailto:J2EE-INTEREST@(protected)] On Behalf Of Frans Verhoef >?Sent: Friday, 8 July 2005 11:51 AM To: J2EE-INTEREST@(protected) >?Subject: Re: Parse integer out of string > >?Just make sure that you define yourInt outside the if statement. To >?be more precise: > >?int yourInt; >?if(m.find()){ >?int yourInt >?Integer.parseInt(yourString.substring(m.start(),m.end())); }else{ >?// take your action in case no integer found in yourString } > >?On Fri, 8 Jul 2005 11:21:22 +1000, Taco wrote: >>?Thanks that's sort of what I put together as well. >> >>?Using your example: >> >>?String yourString = "A string that contains number 30 that needs >>?to be parsed out"; Pattern p = Pattern.compile("\\d+"); Matcher m >>?= p.matcher(yourString); if(m.find()) { int yourInt >>?Integer.parseInt (yourString.substring(m.start(), m.end())); } >>?System.out.println("result: " + yourInt); >> >>?I got: >> >>?java.lang.Error: Unresolved compilation problem: >>?yourInt cannot be resolved >> >>?at SSLTransportLayer08.getResponse(SSLTransportLayer08.java:149) >>?at SSLTransportLayer08.main(SSLTransportLayer08.java:74) >> >>?Which means it never found anything.. ??? >>?I'm puzzled.. >> >>?-- --Original Message-- -- >>?From: A mailing list for Java(tm) 2 Platform, Enterprise Edition >>?[mailto:J2EE-INTEREST@(protected)] On Behalf Of Frans Verhoef >>?Sent: Friday, 8 July 2005 11:18 AM To: J2EE-INTEREST@(protected) >>?Subject: Re: Parse integer out of string >> >>?Actually not J2EE, but here we go: >> >>?String yourString = "A string that contains number 30 that needs >>?to be parsed out"; Pattern p = Pattern.compile("\\d+"); Matcher m >>?= p.matcher(yourString); if(m.find()){ >>?int yourInt = Integer.parseInt(yourString.substring(m.start(), >>?m.end())); } >> >>?Cheers >>?Frans >> >>?=====================================================================>>? === == To unsubscribe, send email to listserv@(protected) and >>?include in the body of the message "signoff J2EE-INTEREST". ?For >>?general help, send email to listserv@(protected) and include in >>?the body of the message "help". >> > >?=====================================================================>?===== To unsubscribe, send email to listserv@(protected) and >?include in the body of the message "signoff J2EE-INTEREST". ?For >?general help, send email to listserv@(protected) and include in >?the body of the message "help". > >?=====================================================================>?===== To unsubscribe, send email to listserv@(protected) and >?include in the body of the message "signoff J2EE-INTEREST". ?For >?general help, send email to listserv@(protected) and include in >?the body of the message "help".
==========================================================================To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|