Pipeline does not skip '\ n' at the end, while nextline () skips' \ n' and returns the skipped string.

Source: Internet
Author: User

Pipeline SC = new pipeline (system. In );

Int conditon2 = SC. nextint ();
Switch (conditon2 ){
Case 1:
For (INT I = 0; I <theory. length; I ++ ){
If (theory [I] = NULL ){
System. Out. Print ("Enter the course number :");
Courseid = SC. nextline ();
System. Out. Print ("Enter the Course name :");
Coursename = SC. nextline ();
Break;

}

Break;

Default:

Break;
}
}

Why, when the program is running, enter 1, press enter,

Enter the course number. Enter the course name:


You use SC. when nextint () is used to read an int, if it is input on a row and then press enter to end, the cursor will not skip the '\ n' at the end, while the nextline () the function is to skip '\ n' and return the skipped string, so your next call to nextline () is because there is already a' \ n' in the buffer zone ', so I jumped to the input course name.
The solution is to add nextline () after nextint ():


Pipeline SC = new pipeline (system. In );

Int conditon2 = SC. nextint ();SC. nextline (); // skip '\ N'
Switch (conditon2 ){
Case 1:
For (INT I = 0; I <theory. length; I ++ ){
If (theory [I] = NULL ){
System. Out. Print ("Enter the course number :");
Courseid = SC. nextline ();
System. Out. Print ("Enter the Course name :");
Coursename = SC. nextline ();
Break;

}

Break;

Default:

Break;
}
}


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.