Java to find consecutive numbers in a string as a whole and return consecutive numbers and the total number of integers

Source: Internet
Author: User

public static void Main (string[] args) {string strnumbers = "0123456789";//System.out.println used for judging the number ("Please enter a string:"); String string = new Scanner (system.in). Next (); Custom input string[] strings = new String[string.length ()];//Custom is String System.out.println ("Custom length = =" +strings.length);// Loop the custom string and place each character into the array for (int i = 0; i < string.length (); i++) {Strings[i] = string.substring (i, i + 1);} int length = 0;//defines the index subscript int suminteter = 0;//computes the integer boolean bool = true; string[] STRs = new String[string.length ()];//then create a new array to hold the continuous number as a sequential data, or loop all the strings for (string str:strings) {// Put the corresponding string into the Strnumbers using IndexOf method to determine whether; Returns -1int index = Strnumbers.indexof (str) If it is returned, or the IF (index = =-1) {//indicates that the current value is not a number strs[length] = str;//assigns the current value directly to the number Group length++;//index Subscript self-increment bool = true; Lock on} else {//= the current value is a number if (bool) {Strs[length] = Str;length++;suminteter++;bool = false;//Lock is closed so the lock is closed because the next character is also the number is run below E LSE Operations} else {//current and previous values are also numbers, run this step//Because the previous index has since been increased, the index must be-1, use concat to link the number to the tail of the previous value strs[length-1] = strs[Length-1].concat (str); }}}system.out.println ("Number of integers" +suminteter+ "a"); for (String str:strs) {if (str! = null) {System.out.println (str);}}}

  

Java to find consecutive numbers in a string as a whole and return consecutive numbers and the total number of integers

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.