Subject: Parse integer out of string 2005-07-07 - By Frans Verhoef
Back 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".
|
|