Whether the Java string is a number-wide judgment

Source: Internet
Author: User

The main body of the program is to enter scores and grade according to grade.

But if you use scanner's Nextint, the program crashes when you enter characters in the console, and then try to determine whether the input is a number and then evaluate it.

The procedure is as follows:

 PackageCom.test.demo;ImportJava.util.Scanner; Public classTestone { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner S=NewScanner (system.in); String Str=NULL;//define STR as a global variable so that the while loop can also be usedSYSTEM.OUT.PRINTLN ("Score Assessment:"); //The first cycle of death is used to keep the grade rating running .         while(true){            BooleanCflag =true;//for the second cycle of death .             while(Cflag) {//Use the dead loop to determine if the character entered is correct                intCount = 0;//if count=str.length, the table name each character is a number, so you can exitSystem.out.println ("Please enter your score:"); STR= S.nextline ();//getting a string from the console                                Char[] CHS = Str.tochararray ();//converting a string to a character array                 for(inti = 0;i<chs.length;i++) {//Stupid Method//the Smart Way is: if (str.matches (reqex: "[0-9]*"))                    if(! (chs[i]>= ' 0 ' &&chs[i]<= ' 9 ') {System.out.println ("The format of the input is incorrect, please re-enter:");  Break; } Count++; }                if(Count==str.length ()) {//It's all numbers.Cflag =false; }} Integer Grade= Integer.parseint (str);//Convert a string to an integer typeSystem.out.println (grade); if(grade>95&&grade<=100) System.out.println ("Great, your grades are: excellent"); Else if(grade>70&&grade<=95) System.out.println ("Great, your grades are: Medium"); Else if(grade>60&&grade<=70) System.out.println ("Great, your grades are: qualified"); Else if(GRADE&LT;=60&AMP;&AMP;GRADE&GT;40) System.out.println ("You failed the Grade!") "); Else if(grade<=40&&grade>=0) System.out.println ("I'm sorry you have to study again!") "); Else{System.out.println ("Input of the score is not the same as the law, please re-enter:"); Continue; } System.out.println ("Do you want to quit?" y/n "); Scanner Ys=NewScanner (system.in); String ex= Ys.nextline (). toUpperCase ();//Convert a string to uppercase            if(Ex.equals ("Y") {System.out.println ("OK, quit success!" ");  Break; }            Else{                Continue; }        }    }}

Whether the Java string is a number-wide judgment

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.