实例1:
// 使用Scanner从键盘读数据并计算平均值
import java.util.*;
class AvgNums {
public static void main(String args[]) {
Scanner conin = new Scanner(System.in);
int count = 0;
double sum = 0.0;
System.out.println("Enter numbers to average.");
// 读取数据并求和,直到遇到一个不是有效的double标记为止.
while(conin.hasNext()) {
if(conin.hasNextDouble()) {
sum += conin.nextDouble();
count++;
}
else {
String str = conin.next();
if(str.equals("done")) break;
else {
System.out.println("Data format error.");
return;
}
}
}
System.out.println("Average is " + sum / count);
}
}
运行结果:
C:\java>java AvgNums
Enter numbers to average.
45
4.5
23.12
done
Average is 24.206666666666667</td>
</tr>
<tr>
<td height="20" colspan="2">
实例2
// 使用Scanner从文件读数据并计算平均值,Scanner读一个源的方法适合读另一个源.
import java.util.*;
import java.io.*;
class AvgFile {
public static void main(String args[])
throws IOException {
int count = 0;
double sum = 0.0;
// 创建文件和数据
FileWriter fout = new FileWriter("test.txt");
fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
// 读文件中的数据并求和.
while(src.hasNext()) {
if(src.hasNextDouble()) {
sum += src.nextDouble();
count++;
}
else {
String str = src.next();
if(str.equals("done")) break;
else {
System.out.println("File format error.");
return;
}
}
}
fin.close();
System.out.println("Average is " + sum / count);
}
}
运行结果:
C:\java>java AvgFile
Average is 6.2
实例3
// 使用 Scanner从文件中读取各种类型的数据
import java.util.*;
import java.io.*;
class ScanMixed {
public static void main(String args[])
throws IOException {
int i;
double d;
boolean b;
String str;
// 创建一个供Scanner读取的文件
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing Scanner 10 12.2 one true two false");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
// 读直到文件尾,读取之前必须检查一下哪种数据类型可得.
while(src.hasNext()) {
if(src.hasNextInt()) {
i = src.nextInt();
System.out.println("int: " + i);
}
else if(src.hasNextDouble()) {
d = src.nextDouble();
System.out.println("double: " + d);
}
else if(src.hasNextBoolean()) {
b = src.nextBoolean();
System.out.println("boolean: " + b);
}
else {
str = src.next();
System.out.println("String: " + str);
}
}
fin.close();
}
}
运行结果:
C:\java>java ScanMixed
String: Testing
String: Scanner
int: 10
double: 12.2
String: one
boolean: true
String: two
boolean: false
实例4
// 设置定界符
// Scanner基于一组定界符定义标记的开始与结束,默认的定界符是空白字符,用useDelimiter()可改变定界符
import java.util.*;
import java.io.*;
class SetDelimiters {
public static void main(String args[])
throws IOException {
int count = 0;
double sum = 0.0;
FileWriter fout = new FileWriter("test.txt");
// 现在用逗号和空格分隔数据.
fout.write("2, 3.4, 5,6, 7.4, 9.1, 10.5, done");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
// 设置定界符为逗号、零个或多个空白,用正则表达式表示。
src.useDelimiter(", *");
// Read and sum numbers.
while(src.hasNext()) {
if(src.hasNextDouble()) {
sum += src.nextDouble();
count++;
}
else {
String str = src.next();
if(str.equals("done")) break;
else {
System.out.println("File format error.");
return;
}
}
}
fin.close();
System.out.println("Average is " + sum / count);
}
}
运行结果:
C:\java>java SetDelimiters
Average is 6.2
实例5
/* 示范findInLine()方法,该方法在文本的下一行搜索指定的模式。如果找到该模式,匹配标记被甩掉并返回
*方法签名如下
*String findInLine(Pattern pattern)
*String findInLine(String pattern)
*/
import java.util.*;
class FindInLineDemo {
public static void main(String args[]) {
String instr = "Name: Tom Age: 28 ID: 77";
Scanner conin = new Scanner(instr);
// 查找age.
conin.findInLine("Age:");
if(conin.hasNext())
System.out.println(conin.next());
else
System.out.println("Error!");
}
}
运行结果:
C:\java>java FindInLineDemo
28
</td>
</tr>
</table>
</td>
</tr>
<tr>
↑返回目录
前一篇: 使用设计模式改善程序结构
后一篇: “中文问题没商量”之Ant中的中文问题