输入:

 1 import java.util.Scanner;
 2 
 3 public class TextScanner {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int nextValue, sum = 0;
11         Scanner kpInput = new Scanner(System.in);//创建Scanner对像
12         kpInput.useDelimiter("\\s");//设置分隔符
13         while (kpInput.hasNextInt()) {//使用hasNextInt()方法判断是否还有整数
14             nextValue = kpInput.nextInt();//读取整数流中的下一个整数
15             sum += nextValue;
16         }
17         kpInput.close();
18         System.out.println("Sum:" + sum);
19 
20     }
21 
22 }

运算结果:

输出:
 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.util.Scanner;
 4 
 5 
 6 public class Text {
 7     public static void main(String[] args) {
 8         //从文件中逐行读取数据
 9         String name;
10         int age;
11         Scanner fileInput;
12         File inFile = new File("src\\Ages.dat");
13         try {
14             fileInput = new Scanner(inFile);
15             while(fileInput.hasNext()){
16                 name = fileInput.next();
17                 age = fileInput.nextInt();
18                 System.out.printf("%s is %d years old.\n",name,age);
19             }
20             fileInput.close();
21             
22         } catch (FileNotFoundException e) {
23             // TODO: handle exception
24             e.printStackTrace();
25         }
26     }
27 }

运算结果:

 

 
posted on 2016-03-28 18:52  软二2014330222孙小晶  阅读(178)  评论(0编辑  收藏  举报