Problem: Reading specific types of data from the keyboard (using scanner to read int types)

Source: Internet
Author: User

ImportJava.util.Scanner; Public classscannerinttest{ Public Static voidmain (String [] args) {intnum1,num2; NUM1= Getintnumber ();//Call Function Getintnumber () to get an integer entered from the keyboardnum2 =Getintnumber (); System.out.println ("First number" +num1+ "-----" + "second number" +num2);//print the two int integers you entered    }     Public Static intGetintnumber () {intIntnumber;//the definition returns the required int integerString strtest;//define strtest to make type judgmentsSystem.out.println ("Please enter an integer:");//Prompt user to enter an integerScanner in =NewScanner (system.in);//instantiate scanner, a field that is used to read from the keyboard, separated by spacesStrtest = In.next ();//reads a space-delimited string of input         while(true){//use loops to determine if the input is correct and prompt for re-entry            Try{intnumber=Integer.parseint (strtest);  Break;//must have the logic to terminate the loop code, or error}Catch(Exception e) {System.out.println ("Input is illegal!" Please re-enter an integer: "); Strtest=In.next (); }} System.out.println ("You entered:" +intnumber); returnintnumber; }}

Another: How to change the class name in Eclipse,--right-click Class, Refactor >> Rename

Problem: Reading specific types of data from the keyboard (using scanner to read int types)

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.