A small problem with scanner scanning console input in Java

Source: Internet
Author: User

 PackageCOM.HXL;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter an integer and a string:"); intAA =Sc.nextint (); String SS=sc.nextline ();System.out.println ("The integer you entered is:" + AA + "The string you entered is:" +SS); }}

"Problem" At this point the console will no longer accept input after entering the first integer to enter ...

The Modified code:

 PackageCOM.HXL;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter an integer and a string:"); intAA =Sc.nextint (); String SS=Sc.nextline (); String SS1=Sc.nextline (); System.out.println ("The integer you entered is:" + AA + "The string you entered is:" +SS1); }}

"Explain" the escape character of carriage return in Windows is \r\n,scanner when it hits the full string of strings it encounters, it reads, and the program goes backwards ...

"Other methods" can be used to receive all data in a string, and then the corresponding conversion.

A small problem with scanner scanning console input in Java

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.