How to write in the Java language the difference between the next () and nextline () HDU topics

Source: Internet
Author: User

Java input with scanner class;

There are two functions in the Scanner class: (function called method in Java)

1) Next () method, encounter a space or a carriage return to end the input;

2) The Nextline () method only ends the input if it encounters a carriage return character.

These two methods occupy two different streams respectively. If you want to enter a data, then you can only select a stream, if used two, then only the first useful, and the data is in the first stream.

If you use the Nextline () method, you sometimes want to pick up the carriage return character,

For example:

Import Java.util.Scanner;
public class AA {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int a = Sc.nextint ();//input 1
Sc.nextline ();//Receive carriage return, here only with Sc.nextline ();
String B = sc.nextline ();//Input 2
System.out.println ("A:" +a+ "--b:" +b);//Output A:1--b:2
}
}

Using the next () method, you do not need to receive a carriage return,

For example:

Import Java.util.Scanner;
public class AA {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int a = Sc.nextint ();//input 1
String B = Sc.next ();//Input 2
System.out.println ("A:" +a+ "--b:" +b);//Output A:1--b:2
}
}





How to write in the Java language the difference between the next () and nextline () HDU topics

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.