Java input numbers are stored in an array (with character detection)

Source: Internet
Author: User

This time is an enhanced version of whether the last character detection is all digital.

You are welcome to point out the shortcomings of friends or the great God.

 Packagecom.hw.h817;ImportJava.util.Scanner; Public classCheck {public static void main (string[] args){Scanner s=NewScanner (system.in); //define the pre and STR outside, if the definition in the loop goes wrongString[] both =NewString[6];        String Pre; int[] str =New int[6]; intCount = 0;//used to exit a loop                 while(true) {System.out.println ("Please enter 6 numbers in turn"); Count= 0;//if the input character is not a number, it is counted back from if             for(inti=0;i<6;i++) {Pre= S.next ();//Receive String                if(! (Pre.matches ("\\d+"))) {//non (pre is all numbers)SYSTEM.OUT.PRINTLN ("Input + pre +" is not a number, please re-enter ");  Break;//this exits the for loop, and then executes the while loop again, re-entering the number                }                Else{Str[i]= Integer.parseint (pre);//converts a numeric string to an int type number} Count++;//count will only be +1 after each data entry is performed            }            if(count==6) Break;//This exit is the while loop.        }         for(inti=0;i<6;i++) {//Output Print Numbers            if(i==0) {System.out.print (str[i]); }            Else{System.out.print (","+Str[i]); }        }    }}

Java input numbers are stored in an array (with character detection)

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.