/** * The following static method is implemented: Returns the value of the first occurrence of the number in string S. * * If no number is found, return-1 For example: * s = "ABC24US43" returns 2 * s = "82445ADB5" returns 8 * s = "AB" returns-1 * * The following static method is implemented: Returns the value of the first occurrence of the number in string S. * */ Public The first number of class { Public Static int Getfirstnum(String s) {if(s = =NULL|| S.length () = =0){return-1; }Charc = S.charat (0);if(c >=' 0 '&& C <=' 9 '){returnS.charat (0) -' 0 ';//minus ' 0 ' after the specific value}returnGetfirstnum (S.substring (1));//Truncate the first character of a string} Public Static void Main(string[] args) {String S1 ="ABC24US43";//returns 2String s2 ="82445ADB5";//returns 8String s3 ="AB";//Then return-1System.out.println (Getfirstnum (S1)); System.out.println (Getfirstnum (S2)); System.out.println (Getfirstnum (S3)); }}
Java algorithm-first number