交互程序 Scanner类
交互程序
1. Scanner类是标准Java类库的类
(1)提供一些方法用于交互式读入不同类型的输入数据,输入可以是不同的数据源,包括用户键入的数据和保存在文件中的数据(怎么弄)。
(2)将一个字符串解析为若干字串。(不知道该怎么用)
2. (1)构造方法,创建新的Scanner对象,从指定的数据源中获取数据
Scanner(InputStream source)
Scanner(File source)
Scanner(String source)
(2)String nextLine() //以字符串类型返回当前行剩余的所有输入数据项
String next() //以字符串类型返回下一个输入数据项
例1:
package seven;
import java.util.Scanner;
public class Echo {
public static void main(String arge[]){
String message1, message2 ;
Scanner scan = new Scanner(System.in);
//创建Scanner类的对象,以便于引用其方法。
//System.in对象代表标准输入流设备,默认时为键盘。
System.out.print("Enter a line of text: ");
message1 = scan.nextLine();
//以字符串类型返回当前行剩余的所有输入数据项
System.out.println("You entered: \""+ message1 +"\"");
System.out.print("Enter a word of text: ");
message2 = scan.next(); //以字符串类型返回下一个输入数据项
System.out.println("You entered: \""+ message2 +"\"");
}
}
输出结果:
Enter a line of text: i love java
You entered: "i love java"
Enter a word of text: i love java
You entered: "i"
(3)返回指定类型的下一个数据输入项;
如果下一个数据项与指定的类型不一致,则将抛出异常InputMismatchException
int nextInt();
short nextShort();
long nextLong();
float nextFloat();
double nextDouble();
byte nextByte();
boolean nextBoolean();
例2:
package seven;
import java.util.Scanner;
public class Echo {
public static void main(String arge[]){
Scanner scan = new Scanner(System.in);
//创建Scanner类的对象,以便于引用其方法。 //System.in对象代表标准输入流设备,默认时为键盘。
int num1;
System.out.print("Enter a number of intrger: ");
num1 = scan.nextInt();
System.out.println("You entered: "+ num1 );
double num2;
System.out.print("Enter a number of double: ");
num2 = scan.nextDouble();
System.out.println("You entered: "+ num2 );
}
}
输出结果1:
Enter a number of intrger: 12
You entered: 12
Enter a number of double: 12.4
You entered: 12.4
输出结果2:
Enter a number of intrger: 12.3
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at seven.Echo.main(Echo.java:21)
(4)如果Scanner 对象还有输入数据项,则返回true;
反之,不执行hasNext()函数,什么都不返回
boolean hasNext();
例3:
package seven;
import java.util.Scanner;
public class Echo {
public static void main(String arge[]){
Scanner scan = new Scanner(System.in);
String message;
System.out.print("Enter a word of text: ");
message = scan.next(); //以字符串类型返回下一个输入数据项
System.out.println("You entered: \""+ message +"\".");
if(scan.hasNext())
System.out.println(" there are words that haven't been input");
}
}
输出结果1:
Enter a word of text: i love java
You entered: "i".
there are words that haven't been input
输出结果2:
Enter a word of text: ilovejava
You entered: "ilovejava".
例4:
package seven;
import java.util.Scanner;
public class Echo {
public static void main(String arge[]){
Scanner scan = new Scanner(System.in);
String message;
System.out.print("Enter a word of text: ");
message = scan.next();
System.out.println("You entered: \""+ message +"\".");
if(scan.hasNext())
System.out.println("there are words that haven't been input");
int number;
System.out.print("Enter a number: ");
number = scan.nextInt();
System.out.println("You entered the number:"+number);
if(scan.hasNext())
System.out.println("there are numbers that haven't been input");
}
}
输出结果1:
Enter a word of text: i love java
You entered: "i".
there are words that haven't been input
Enter a number: Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at seven.Echo.main(Echo.java:16)
分析:第一次输入i love java后,输出的只有i,love 和 java两个单词在缓冲区里;在遇到scan.nextInt()函数,需要第二次输入时,由于缓冲区里还有数据,则不需要从键盘再输入,直接读缓冲区里的数据,则出现缓冲区里的数据类型与要求的数据类型不一致的异常。
输出结果2:
Enter a word of text: ilovejava
You entered: "ilovejava".
例5:
package seven;
import java.util.Scanner;
public class Echo {
public static void main(String arge[]){
Scanner scan = new Scanner(System.in);
String message4;
boolean bool = false;
System.out.print("Enter some words: ");
message4 = scan.next();
System.out.println("You entered:\""+message4+"\".");
bool = scan.hasNext();
if(bool)
System.out.println("there are words haven't been input");
else
System.out.println("all words have been input");
}
}
输出结果1:
Enter some words: i love java
You entered:"i".
there are words haven't been input
输出结果2:
Enter some words: ilovejava
You entered:"ilovejava".
??结合例4的输出结果2和例5的输出结果2,我怀疑scan.hasNext()这个函数在所有内容都输出后,就直接跳出了当前程序,不再运行。
例6:
package seven;
import java.util.Scanner;
public class Echo{
public static void main(String arge[]){
Scanner scan = new Scanner(System.in);
//创建Scanner类的对象,以方便引用其方法;
//System.in对象代表标准输入流对象,默认为键盘。
String message5;
int number2;
System.out.print("Enter a string:");
message5 = scan.next();
System.out.println("You entered:\""+message5+"\".");
System.out.print("Enter a number:");
number2 = scan.nextInt();
System.out.println("You entered: "+number2);
}
}
输出结果1:
Please enter a string:ilovejava
You entered:"ilovejava".
Please enter a number:1234 456
You entered:"1234".
输出结果2:
Please enter a string:i love java
You entered:"i".
Please enter a number:Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at seven.Echo.main(Echo.java:17)
??怎么知道一个字符串或数字串有多少个字符或数字单元组成。那么我就可以决定要输入几个字符数字单元,已经输入了多少个,还有多少个没有输入。