參考鏈接: Java中Scanner和BufferReader類之間的區別
從開始學習Java就用了scanner,因為比較簡單每當遇到空格鍵或者換行鍵則讀取下一個字符,一般用法?
? while(input.hasNextInt()){ int n = input.nextInt(); int t = input.nextInt(); int c = input.nextInt(); int[] a = new int[n]; for(int i = 0;i < n;i++){ a[i]=input.nextInt(); }?
?這樣就可以讀取若干行以空格鍵或者換行鍵輸入,但是今天做一個在線編程時發現bufferreader比scanner快,雖然比scanner占用內存多一些?
?bufferreader的輸入只能使用readline()的方式讀取一行文本,如果想將BufferedReader .readLine()的文本(默認是字符串類型)轉換成其他類型的話,需要調用相應的方法(比如說想換成int類型的話,調用Integer.parseInt(BufferedReader .readLine()方法去轉換格式))??
?
? BufferedReader br =?
? new?
? BufferedReader(
? new?
? InputStreamReader(System.in));
??
?
? ? ? ? ??
? String str;
??
?
? ? ? ? ??
? while
? ((str=br.readLine())!=
? null){
??
?
? ? ? ? ? ? ??
? String[] s = str.trim().split(
? " "
? );
??
?
? ? ? ? ? ? ??
? int?
? n = Integer.parseInt(s[
? 0
? ]);
??
?
? ? ? ? ? ? ??
? int?
? t = Integer.parseInt(s[
? 1
? ]);
??
?
? ? ? ? ? ? ??
? int?
? c = Integer.parseInt(s[
? 2
? ]);
??
?
? ? ? ? ? ? ??
? int
? [] value =?
? new?
? int
? [n];
??
?
? ? ? ? ? ? ??
? if
? ((str=br.readLine())!=
? null
? ){
??
?
? ? ? ? ? ? ? ? ??
? String[] s2 = str.trim().split(
? " "
? );
??
?
? ? ? ? ? ? ? ? ??
? for
? (
? int?
? i =
? 0
? ;i<n;i++){
??
?
? ? ? ? ? ? ? ? ? ? ??
? value[i] = Integer.parseInt(s2[i]);
??
?
? ? ? ? ? ? ? ? ??
? }
??
?
? ? ? ? ? ? ??
? }
??
?
? 2.trim()的用法
??
?
? trim是去掉字符串首尾的空格
??
?
? 3.parseInt()函數
??
?
? Integer.parseInt(String s)將string返回int數據
??
?
? Integer.parseInt(String s,int i)將i進制數據轉成10進制
??
?
轉載于:https://www.cnblogs.com/lxy1998/p/6775533.html