1. java中如何用scanner实现输入数据
首先创建一个Scanner对象s,构造函数的初始值为System.in,再利用s.nextInt()来接收一个int型数据ival的值,用s.nextLine()来接收String类型的name,然后分别输出。
publicclassInputFromConsole{
publicstaticvoidmain(String[]args){
Scanners=newScanner(System.in);
Stringname=s.nextLine();
intival=s.nextInt();
System.out.println(ival+","+name);
}
}
2. java中怎么用scanner
使用该类创建一个对象.
ScannerSc=new Scanner(System.in);
然后Sc对象调用下列方法(函数),读取用户在命令行输入的各种数据类型: next.Byte(),nextDouble(),nextFloat,nextInt(),nextLin(),nextLong(),nextShot() 。这些方法执行时都会造成堵塞,等待用户在命令行输入数据回车确认。
例如,拥护在键盘输入12.34,hasNextFloat()的值是true,而hasNextInt()的值是false。NextLine()等待用户输入一个文本行并且回车,该方法得到一个String类型的数据。
例子代码:
(2)java中的输入scanner扩展阅读:
Scanner的构造器支持多种方式,可以从字符串(Readable)、输入流、文件等等来直接构建Scanner对象,有了Scanner了,就可以逐段(根据正则分隔式)来扫描整个文本,并对扫描后的结果做想要的处理。
下面是一些API函数的用法:
delimiter():
返回此 Scanner 当前正在用于匹配分隔符的 Pattern。
hasNext() :
判断扫描器中当前扫描位置后是否还存在下一段。
hasNextLine() :
如果在此扫描器的输入中存在另一行,则返回 true。
next() :
查找并返回来自此扫描器的下一个完整标记。
nextLine() :
此扫描器执行当前行,并返回跳过的输入信息。
3. java语言中的scanner是什么意思用来干嘛
scanner是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matcher类之间的某种结合。
由于任何数据都必须通过同一模式的捕获组检索或通过使用一个索引来检索文本的各个部分。于是可以结合使用正则表达式和从输入流中检索特定类型数据项的方法。这样,除了能使用正则表达式之外,Scanner类还可以任意地对字符串和基本类型(如int和double)的数据进行分析。借助于Scanner,可以针对任何要处理的文本内容编写自定义的语法分析器。例如:
public class NextTest{
public static void main(String[] args) {
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.print("请输入第一个字符串:");
s1=sc.nextLine();
System.out.print("请输入第二个字符串:");
s2=sc.next();
System.out.println("输入的字符串是:"+s1+" "+s2);
}
}
运行结果是:
请输入第一个字符串:home
请输入第二个字符串:work
输入的字符串是:home work
(3)java中的输入scanner扩展阅读:
scanner中关于nextInt()、next()和nextLine()的理解
nextInt(): it only reads the int value, nextInt() places the cursor(光标) in the same line after reading the input.(nextInt()只读取数值,剩下” ”还没有读取,并将cursor放在本行中)
next(): read the input only till the space. It can’t read two words separated by space. Also, next() places the cursor in the same line after reading the input.(next()只读空格之前的数据,并且cursor指向本行)
next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描,当遇见第一个分隔符或结束符(空格或换行符)时,结束扫描,获取扫描到的内容,即获得第一个扫描到的不含空格、换行符的单个字符串。
nextLine(): reads input including space between the words (that is, it reads till the end of line ). Once the input is read, nextLine() positions the cursor in the next line.
nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。
4. java中如何使用scanner来实现数据的输入
Scanner scan=new Scanner(System.in);
int a=scan.nextInt();
String a=scan.nextLine();
double a=scan.nextDouble();
自己去查API吧。还有好多。
上面的程序没有问题。是不是你的环境设置有问题?
还有,把你上面那个“Scanner scanner;”去掉,你下面的代码都有“Scanner scan=new Scanner(System.in); ”了,还定义上面那个干吗?
5. java中scanner用法
上面的不写了
写在main方法中的
Scanner
scan=new
Scanner(System.in);
String
str=scan.next();
if(str.equals("s1")){
System.out.println(s1.学号);
System.out.println(s1.名字);
.....
}else
if(str.equals("s2")){
......
}else
if(....
.....
ok
明白了吧
6. Scanner在java中有什么用法怎么用
1、首先在一个java工程下创建一个类名的ScannerDemo的类。
(6)java中的输入scanner扩展阅读:
Scanner类常用方法
1、String next():接收控制台输入的字符串(备注:不能将空格作为字符串接收);
2、String nextLine():接收控制台输入的字符串;
3、int nextInt():接收控制台输入的int类型的数据;
4、double nextDouble:接收控制台输入的double 类型的数据;
5、boolean nextBoolean():接收控制台输入的boolean 类型的数据;
6、输入char类型的数据;
Scanner类没有直接输入char类型的方法,可以通过charAt()方法从next()或nexyLine()获取。
7. java中 Scanner是干嘛的
Scanner是Java提供的读取系统输入(如键盘输入)或者读取文件流的一个类。
读取键盘输入时,与BufferedReader基本功能相同。
例如从键盘读取10个整数到一个数组里
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner scan = new Scanner(System.in);
int count = 0;
while(scan.hasNext()){
list.add(scan.nextInt());
count++;
if(count==10) break;
}
这就行了。
如果想知道更多用法,可以查看API中Scanner用法。
可以参考下面链接:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html