java.util.Scanner獲取輸入的整數(shù),長(zhǎng)整型,字符串等:
代碼如下:
/package com.color.program.ball;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
//receive string
String str = s.next();
//receive integer
Integer i = s.nextInt();
//receive double
Double d = s.nextDouble();
System.out.println(str+i+d);
}
}
如用三元運(yùn)算符判斷一個(gè)數(shù)是奇數(shù)還是偶數(shù):
import java.util.Scanner;
public class Ou {
public static void main(String[] args) {
System.out.println("請(qǐng)輸入一個(gè)整數(shù):");
Scanner reader = new Scanner(System.in);
long a = reader.nextLong();
String str = (a%2 )==0 ? "偶數(shù)":"奇數(shù)";
System.out.println("結(jié)果是:"+str);
}
}
一、掃描控制臺(tái)輸入
這個(gè)例子是常常會(huì)用到,但是如果沒(méi)有Scanner,你寫(xiě)寫(xiě)就知道多難受了。
當(dāng)通過(guò)new Scanner(System.in)創(chuàng)建一個(gè)Scanner,控制臺(tái)會(huì)一直等待輸入,直到敲回車(chē)鍵結(jié)束,把所輸入的內(nèi)容傳給Scanner,作為掃描對(duì)象。如果要獲取輸入的內(nèi)容,則只需要調(diào)用Scanner的nextLine()方法即可。
代碼如下:
/**
* 掃描控制臺(tái)輸入
*
* @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("請(qǐng)輸入字符串:");
while (true) {
String line = s.nextLine();
if (line.equals("exit")) break;
System.out.println(">>>" + line);
}
}
}
請(qǐng)輸入字符串:
234
>>>234
wer
>>>wer
bye
>>>bye
exitProcess finished with exit code 0
先寫(xiě)這里吧,有空再繼續(xù)完善。
二、如果說(shuō)Scanner使用簡(jiǎn)便,不如說(shuō)Scanner的構(gòu)造器支持多種方式,構(gòu)建Scanner的對(duì)象很方便。
可以從字符串(Readable)、輸入流、文件等等來(lái)直接構(gòu)建Scanner對(duì)象,有了Scanner了,就可以逐段(根據(jù)正則分隔式)來(lái)掃描整個(gè)文本,并對(duì)掃描后的結(jié)果做想要的處理。
代碼如下:
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.MatchResult;
public class TestScanner {
public static void main(String[] args) throws FileNotFoundException {
// 鍵盤(pán)輸入
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt());
System.out.println("---------------");
// 文本掃描
Scanner sc2 = new Scanner(new File("D://1.txt"));
while (sc2.hasNextDouble()) {
System.out.println(sc2.nextDouble());
}
System.out.println("---------------");
// 正則解析
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
System.out.println("---------------");
// 正則-匹配組
String input2 = "1 fish 2 fish red fish blue fish";
Scanner s2 = new Scanner(input2);
s2.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s2.match();
for (int i = 1; i <= result.groupCount(); i++)
System.out.println(result.group(i));
s.close();
}
}
以下代碼使 long 類(lèi)型可以通過(guò) myNumbers 文件中的項(xiàng)分配:
代碼如下:
Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}
三、Scanner默認(rèn)使用空格作為分割符來(lái)分隔文本,但允許你指定新的分隔符(支持正則表達(dá)式)
使用默認(rèn)的空格分隔符:
代碼如下:
public static void main(String[] args) throws FileNotFoundException {
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());
}
}
123
asdf
sd
45
789
sdf
asdfl,sdf.sdfl,asdf
......asdfkl
lasProcess finished with exit code 0
將注釋行去掉,使用空格或逗號(hào)或點(diǎn)號(hào)作為分隔符,輸出結(jié)果如下:
123
asdf
sd
45
789
sdf
asdfl
sdf
sdfl
asdfasdfkllas
Process finished with exit code 0
再來(lái)個(gè)例子,根據(jù)pattern字符串來(lái)匹配
代碼如下:
package test;
import java.util.Scanner;
import java.util.regex.MatchResult;
public class TestScanner2 {
public static void main(String[] args) {
String data = "\n" +
"\n" +
"\n" +
"\n" +
"[Next log section with different format]";
Scanner s = new Scanner(data);
String pattern = "(\\d+[.]\\d+[.]\\d+[.]\\d+)@(\\d{1,2}/\\d{1,2}/\\d{4})";
while(s.hasNext(pattern)) {
s.next(pattern);
MatchResult mr = s.match();
System.out.format("ip = %-15s, data= %10s\n", mr.group(1), mr.group(2));
}
}
}
useDelimiter(Pattern pattern)這個(gè)方法是Scanner中用于設(shè)置分隔符的,默認(rèn)情況下scanner分割符是空格,你這 個(gè)程序中就是用正則表達(dá)式來(lái)設(shè)置分隔符,"\\s*fish\\s*"前面的一個(gè)\\s*表示空格出現(xiàn)0次或多次接著出現(xiàn)fish接著出現(xiàn)0個(gè)或多個(gè)空 格,只要scanner掃描遇到的數(shù)據(jù)符合這個(gè)正則表達(dá)式,前面的就當(dāng)一個(gè)數(shù)據(jù)就可以用Scanner中的next()返回取得數(shù)據(jù)。
代碼如下:
//output
ip = 127.0.0.1 , data= 21/10/2005
ip = 128.0.0.11 , data= 3/11/2006
ip = 129.132.111.111, data= 4/2/2007
ip = 130.0.0.1 , data= 15/1/2008
總結(jié):1)多種輸入,F(xiàn)ile,input,System.in,String等
2)與正則結(jié)合使用
3)實(shí)現(xiàn)了Iterator接口
四、一大堆API函數(shù),實(shí)用的沒(méi)幾個(gè)
(很多API,注釋很讓人迷惑,幾乎毫無(wú)用處,這個(gè)類(lèi)就這樣被糟蹋了,啟了很不錯(cuò)的名字,實(shí)際上做的全是齷齪事)
下面這幾個(gè)相對(duì)實(shí)用:
delimiter()
返回此 Scanner 當(dāng)前正在用于匹配分隔符的 Pattern。
hasNext()
判斷掃描器中當(dāng)前掃描位置后是否還存在下一段。(原APIDoc的注釋很扯淡)
hasNextLine()
如果在此掃描器的輸入中存在另一行,則返回 true。
next()
查找并返回來(lái)自此掃描器的下一個(gè)完整標(biāo)記。
nextLine()
此掃描器執(zhí)行當(dāng)前行,并返回跳過(guò)的輸入信息。
五、逐行掃描文件,并逐行輸出
看不到價(jià)值的掃描過(guò)程
代碼如下:
public static void main(String[] args) throws FileNotFoundException {
InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java"));
Scanner s = new Scanner(in);
while(s.hasNextLine()){
System.out.println(s.nextLine());
}
}
package own;
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;
/**
* @author wangpeng
*
*/
public class AutoSubmit {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
...在此省略N行
Process finished with exit code 0
更多信息請(qǐng)查看IT技術(shù)專欄