(转)java.util.Scanner应用详解
* 扫描控制台输入
*
* @author leizhimin 2009-7-24 11:24:47
*/
public class TestScanner {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入字符串:");
while (true) {
String line = s.nextLine();
if (line.equals("exit")) break;
System.out.println(">>>" + line);
}
}
}
234
>>>234
wer
>>>wer
bye
>>>bye
exit
Process finished with exit code 0
{
Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las");
// s.useDelimiter(" |,|\\.");
while (s.hasNext()) {
System.out.println(s.next());
}
}
asdf
sd
45
789
sdf
asdfl,sdf.sdfl,asdf
......asdfkl
las
Process finished with exit code 0
asdf
sd
45
789
sdf
asdfl
sdf
sdfl
asdf
asdfkl
las
Process finished with exit code 0
返回此 Scanner 当前正在用于匹配分隔符的 Pattern。
hasNext()
判断扫描器中当前扫描位置后是否还存在下一段。(原APIDoc的注释很扯淡)
hasNextLine()
如果在此扫描器的输入中存在另一行,则返回 true。
next()
查找并返回来自此扫描器的下一个完整标记。
nextLine()
此扫描器执行当前行,并返回跳过的输入信息。
{
InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java"));
Scanner s = new Scanner(in);
while(s.hasNextLine()){
System.out.println(s.nextLine());
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import com.verisign.uuid.UUID;
/**
* ��һ������թƭ��վ�Զ��ύ������Ϣ�ij����ÿտ�������һ�¡�
* @author wangpeng
*
*/
public class AutoSubmit {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
...在此省略N行
Process finished with exit code 0
import java.util.Arrays;
import java.util.Scanner; public class Scanner02 {
public static void main(String[] args){
Scanner inScanner = new Scanner(System.in);
String string = inScanner.nextLine();
String[] sChar = string.split(",");
//String[] sChar = string.split(" "); //以空格作为分隔符
int[] nums = new int[sChar.length];
for(int i= 0;i<nums.length;i++){
nums[i] = Integer.parseInt(sChar[i]); //string型转换为int型
}
String nString = inScanner.nextLine(); //实现数据多行输入
System.out.println(Arrays.toString(nums)); //输出数组 System.out.println("n:"+Integer.parseInt(nString));
inScanner.close(); } }
运行结果:
5
[1, 2, 3, 4, 5]
n:5
import java.util.Arrays;
import java.util.Scanner; public class Scanner03 { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner = new Scanner(System.in);
int[] nums = new int[5];
/*while(inScanner.hasNext()){
System.out.println(inScanner.nextInt());
}*/
for(int i=0;i<5;i++){ //利用循环来存储数组元素
nums[i] = inScanner.nextInt();
} System.out.println(Arrays.toString(nums));
inScanner.close(); } }
打印结果:
[1, 2, 3, 4, 5]
(转)java.util.Scanner应用详解的更多相关文章
- java.util.Scanner应用详解++扫描控制台输入
java.util.Scanner应用详解 java.util.Scanner是Java5的新特征,主要功能是简化文本扫描.这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java ...
- 国际化,java.util.ResourceBundle使用详解
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
- java.util.ResourceBundle使用详解
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
- java.util.ResourceBundle使用详解(转)
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
- 【转】java.util.ResourceBundle使用详解
原文链接:http://lavasoft.blog.51cto.com/62575/184605/ 人家写的太好了,条理清晰,表达准确. 一.认识国际化资源文件 这个类提供软件国际化的捷径.通 ...
- java.util.Arrays类详解(源码总结)
概述 Arrays类位于java.util包下,是一个对数组操作的工具类.今天详细的看了看Arrays类的4千多行源码,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Array ...
- java.util.concurrent BlockingQueue详解
什么是阻塞队列? 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列.这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空.当队列满时,存储元素的线程会等待队列可用.阻塞 ...
- java.util.ConcurrentModificationException解决详解
异常产生 当我们迭代一个ArrayList或者HashMap时,如果尝试对集合做一些修改操作(例如删除元素),可能会抛出java.util.ConcurrentModificationExceptio ...
- Java Scanner用法详解
一.Scanner类简介 Java 5添加了java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序.它是以前的StringTokenizer和Matcher类之间的某种结合.由于 ...
随机推荐
- spring boot 入门操作(二)
spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...
- Docker Machine 简介
Docker Machine 是什么? Docker Machine 是 Docker 官方提供的一个工具,它可以帮助我们在远程的机器上安装 Docker,或者在虚拟机 host 上直接安装虚拟机并在 ...
- 由SpringMVC中RequetContextListener说起
零.引言 RequetContextListener从名字结尾Listener来看就知道属于监听器. 所谓监听器就是监听某种动作,在其开始(初始化)和结束(销毁)的时候进行某些操作. 由此可以猜测:该 ...
- Oracle Job定时任务的使用详解
oracle中的job能为你做的就是在你规定的时间格式里执行存储过程,定时执行一个任务 .下面是一个小案例,定时每15分钟向一张表插入一条数据 一 1.创建一张测试表 -- Create table ...
- SDS 链表
sds定义 struct sdshdr{ int len int free char buf[] } sds和c语言类似,仍然把字符串的末尾加上一个'.0',但是不会计入总长度,也就是不会对len造成 ...
- DDD领域驱动之干活(四)补充篇!
距离上一篇DDD系列完结已经过了很长一段时间,项目也搁置了一段时间,想想还是继续完善下去. DDD领域驱动之干货(三)完结篇! 上一篇说到了如何实现uow配合Repository在autofac和au ...
- Flash TextField selectable bug block TextEvent.Link solution
There is an old version Felx SDK bug(in my case it's Flex SDK v3.3.0.4852) that when TextField.selec ...
- 持续集成篇-- SonarQube代码质量管理平台的安装
视频教程:http://www.roncoo.com/course/view/85d6008fe77c4199b0cdd2885eaeee53 IP:192.168.4.221 环境:CentOS 6 ...
- session或memcache过期之后跳转到登陆页面并跳出iframe框架
<!--在你想控制跳转的页面,比如login.html中的<head>与</head>之间加入以下代码:--> <script> if (window ...
- 【Android Developers Training】 100. 使用Intent修改联系人数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...