Hangzhou Electric 2015 Postgraduate examination of Computer College second interview written first question Java solution
Import Java.util.scanner;import java.util.regex.matcher;import java.util.regex.pattern;/* Hangzhou 2015 Computer College re-examination pen first question JAV A solution * Author Liu Wangyang QQ 605283073 * Find out: Strings such as: "34223abd#34sb-11--" * in the integer and where-the negative sign before the number, otherwise the character */public class Test5{public stat IC void Main (string[] args) {Scanner in = new Scanner (system.in); string input = "";//Get input string int sum;//sum int num;//number try//join exception handling {while (In.hasnext ()) {sum = 0;//each time the input is summed and the number is cleared by 0 num=0;input=in.nextline ();//Read the user input string reg = "[\\-|\\+]?\\d+";//Regular expression can be preceded with class without + or-after The polygon is one to multiple digits Pattern p = pattern.compile (reg); Matcher m = p.matcher (input); while (M.find ())//if accompany {//System.out.println (Input.substring (M.start (), M.end ())); num = Integer.parseint ( Input.substring (M.start (), M.end ())); sum+=num;//adds the truncated sum}system.out.println ("The sum of the numbers in the input string is:" +sum);} }catch (Exception e) {e.printstacktrace ();} finally//finally closes the stream {in.close ();}}}
Effect:
2015 the first question of the second interview of Hangzhou Electric Computer College the Java language solution