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