Precautions for nextint and nextline in Java Connector

Source: Internet
Author: User
 

Nextint () reads only numbers, but line breaks do not,

When nextint () is used, it must be a numeric character, followed by a line break and nextline (). It ends when the line Delimiter is read, and there is no information before the line separator, therefore, no information can be returned.

Import Java. util. sums; public class sum {/*** @ Param ARGs */public static void main (string [] ARGs) {int n = 0; string S1; scanner S = new partition (system. in); n = S. nextint (); // S1 = S. nextline (); the addition of this sentence is very different from the addition of this sentence. // system. out. println ("N:" + n); int S2 [] = new int [N]; for (INT I = 0; I <n; I ++) {S1 = S. nextline (); S2 [I] = getlinesum (S1) ;}for (INT I = 0; I <n; I ++) {system. out. println (s2 [I]) ;}} public static int getlinesum (string strline) {second s = new second (strline); int sum = 0; while (S. hasnextint () {sum + = S. nextint () ;}return sum ;}}

If no value is added in the marked Red Area

s1=s.nextLine();

Result:

2

1 3

0

4

The result is as follows:

2

1 3

2 4

4

6

The cause of the problem has been explained at the beginning. Pay attention to this simple input/output format.

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.