* 扫描控制台输入
*
* @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]