2015 the first question of the second interview of Hangzhou Electric Computer College the Java language solution

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.