Questions about nextline () that cannot be entered in Java

Source: Internet
Author: User

The following code is used in the after-class exercise

1      Public Static voidMain (string[] args) {2System.out.print ("Enter student number:");3         intStudent_number =input.nextint ();4System.out.print ("Input student Score:");5String scores =input.nextline ();6         int[] Score_arrayi =Getscore (scores);7         intMax_score =Maxscore (score_arrayi);8 grade (Score_arrayi,max_score);9}

However, the following problems may occur in the specific input

That is, after you enter the number of Students 4, press ENTER. Unable to enter student scores, skip directly

1 String scores = Input.nextline ();

After finding the information, I learned that:

Next () must be read to a valid character before the end of the input, to enter a valid character before the space bar, tab or enter key, such as The Terminator, the next () method will automatically remove it, only after entering a valid character, the next () method will be entered after the space bar, The TAB key or enter key is treated as a delimiter or terminator. Simply put, next () finds and returns the next complete tag from this scanner. The complete tag is preceded by the input information that matches the delimited pattern, so the next method cannot get a string with spaces

The Terminator of the Nextline () method is just the ENTER key, which means that the nextline () method returns all characters before the ENTER key, which is a string with spaces.

This means that nextint () ends after reading a valid value, the carriage return is still present after the number is lost, and nextline () reads the carriage return directly to the end, so the result is similar to skipping the input statement

Therefore, in order to solve this problem, you can add a nextline () after Nextint () to complete the code as follows

1      Public Static voidMain (string[] args) {2System.out.print ("Enter student number:");3         intStudent_number =input.nextint ();4System.out.print ("Input student Score:");5 input.nextline ();6String scores =input.nextline ();7         int[] Score_arrayi =Getscore (scores);8         intMax_score =Maxscore (score_arrayi);9 grade (Score_arrayi,max_score);Ten}

Questions about nextline () that cannot be entered in Java

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.