在Java程序中获取输入的最常见方法是通过Scanner类,该类是使用下面给出的语句从Java Utility Package中导入的。
import java.util.Scanner; /*imported at beginning of the java program*/
输入与帮助输入流System.in一起提供
语法:
Scanner KB=new Scanner(System.in); /*Where KB is an object name, you can change it as per your choice*/
对于不同的原始数据类型,Scanner类中提供了不同的输入方法,例如:
| 数据类型 | 方法 | 
|---|---|
| Integer | nextInt() | 
| Double | nextDouble() | 
| Long | nextLong() | 
| Float | nextFloat() | 
| Byte | NextByte() | 
| String | nextLine()/ *允许在字符串之间留空格* /  next()/ *不允许在字符串之间留空格* /  | 
考虑一下程序:
import java.util.Scanner;
class UnexpectedBehaviour
{
	public static void main(String args[])
	{
		Scanner KB=new Scanner(System.in);
		
		int i;
		float f;
		String s;
		
		i=KB.nextInt();
		f=KB.nextFloat();
		s=KB.nextLine();
		
		System.out.println("Output String is : "+s);
		System.out.println("Output Integer is : "+i);
		System.out.println("Output Float is : "+f);
	}
}输出结果
1 8.8 Output String is : Output Integer is : 1 Output Float is : 8.8