java 扫描输入

 到目前为止,从文件或标准输入读取数据还是一件相当痛苦第事情,一般第解决之道就是读入一行文本,对其进行分词,然后使用Integer Double 等类第各种解析方法来解析数据:

//: strings/SimpleRead.java\
package strings;
import java.io.*;

public class SimpleRead {
public static BufferedReader input = new BufferedReader(
new StringReader("Sir Robin of Camelot\n22 1.61803"));
public static void main(String[] args) {
try {
System.out.println("What is your name?");
String name = input.readLine();
System.out.println(name);
System.out.println(
"How old are you? What is your favorite double?");
System.out.println("(input: <age> <double>)");
String numbers = input.readLine();
System.out.println(numbers);
String[] numArray = numbers.split(" ");
int age = Integer.parseInt(numArray[0]);
double favorite = Double.parseDouble(numArray[1]);
System.out.format("Hi %s.\n", name);
System.out.format("In 5 years you will be %d.\n",
age + 5);
System.out.format("My favorite double is %f.",
favorite / 2);
} catch(IOException e) {
System.err.println("I/O exception");
}
}
} /* Output:
What is your name?
Sir Robin of Camelot
How old are you? What is your favorite double?
(input: <age> <double>)
22 1.61803
Hi Sir Robin of Camelot.
In 5 years you will be 27.
My favorite double is 0.809015.
*///:~

上面的方法只能输入出来简单的情况,如果两个输入值在同一行中,事情就不好办了,我们必须分解这个行才能分别翻译所需要第输入值,利用Scanner类可以大大减轻扫描输入第工作负担:

Scanner的构造器可以接受任何类型的输入,包括FIle对象,InputStream,String或者此类中第Readable对象.  Readble表示"具有read()方法的某种东西,BufferedReader也属于这一类.

有了Sacnner,所有的输入,分词以及翻译第操作都隐藏在不同第next方法中,普通第next()方法返回下一个String,所有第基本类型(除char之外)都有对应第next方法,包括BigDecimal和BigInteger,所有第next()方法,只有在找到一个完整第分词之后才会返回.Scannner还有相应第hasNext()方法,用于判断下一个输入分词是否所需类型.

在此例中没有针对IOException添加try块,因为,Scanner有一个假设,在输入结束时会抛出IOException异常,所以Scanner会吧IOException吞掉,不过通过ioException()方法,你可以找到最近发生的异常

//: strings/BetterRead.java
package strings;
import java.util.*;

public class BetterRead {
public static void main(String[] args) {
Scanner stdin = new Scanner(SimpleRead.input);
System.out.println("What is your name?");
String name = stdin.nextLine();
System.out.println(name);
System.out.println(
"How old are you? What is your favorite double?");
System.out.println("(input: <age> <double>)");
int age = stdin.nextInt(); //注意这里没加hasNextInt()方法,如果输入有误程序会抛出异常
double favorite = stdin.nextDouble();
System.out.println(age);
System.out.println(favorite);
System.out.format("Hi %s.\n", name);
System.out.format("In 5 years you will be %d.\n",
age + 5);
System.out.format("My favorite double is %f.",
favorite / 2);//

stdin.ioException().printStackTrace()
}
} /* Output:
What is your name?
Sir Robin of Camelot
How old are you? What is your favorite double?
(input: <age> <double>)
22
1.61803
Hi Sir Robin of Camelot.
In 5 years you will be 27.
My favorite double is 0.809015.
*///:~

猜你喜欢

转载自www.cnblogs.com/jiangfeilong/p/10332215.html