課程
/后端開(kāi)發(fā)
/Java
/Java入門(mén)第二季 升級(jí)版
如何編寫(xiě)記錄一個(gè)文檔中某個(gè)字?jǐn)?shù)出現(xiàn)的次數(shù)
2015-12-04
源自:Java入門(mén)第二季 升級(jí)版 9-8
正在回答
????????public?static?void?main(String[]?args)?{ print("d:/test.txt","b"); } public?static?void?print(String?fileName,String?str){ try?{ FileInputStream?inputStream?=?new?FileInputStream(fileName); InputStreamReader?reader?=?new?InputStreamReader(inputStream); StringBuffer?sb?=?new?StringBuffer(); while(reader.ready()){ sb.append((char)reader.read()); } int?count?=?0; int?fromIndex?=0; while(true){ fromIndex=sb.indexOf(str,?fromIndex); if(fromIndex!=-1){ fromIndex++; count++; }else{ break; } } System.out.println(sb.toString()); System.out.println(count); reader.close(); inputStream.close(); }?catch?(FileNotFoundException?e)?{ e.printStackTrace(); }?catch?(IOException?e)?{ e.printStackTrace(); } }
小豪呵呵 提問(wèn)者
/* ?*?讀取一個(gè)文檔里的一個(gè)字符出現(xiàn)的次數(shù) ?*/ public?static?void?print(String?fileName,String?str){ try?{ //?構(gòu)建FileInputStream對(duì)象 FileInputStream?inputStream?=?new?FileInputStream(fileName); //?構(gòu)建InputStreamReader對(duì)象,可以指定編碼格式 InputStreamReader?reader?=?new?InputStreamReader(inputStream); StringBuffer?sb?=?new?StringBuffer(); while(reader.ready()){ sb.append((char)reader.read());//?轉(zhuǎn)成char加到StringBuffer對(duì)象中 } int?count?=?0;//出現(xiàn)的次數(shù) int?fromIndex?=0;//indexOf中查詢參數(shù) while(true){ fromIndex=sb.indexOf(str,?fromIndex);//返回該字符在文檔中出現(xiàn)的位置,int if(fromIndex!=-1){ //找到了該字符,查詢的索引+1,出現(xiàn)的次數(shù)+1 fromIndex++; count++; }else{ break; } } System.out.println(sb.toString()); System.out.println(count); reader.close();//?關(guān)閉讀取流 inputStream.close();//?關(guān)閉輸入流,釋放系統(tǒng)資源 }?catch?(FileNotFoundException?e)?{ e.printStackTrace(); }?catch?(IOException?e)?{ e.printStackTrace(); } }
?System.out.println(new CharCounter().counter("LOVELOVEYOU",'O'));//這一行是什么意思?
public class CharCounter{ public static int counter(String s,char c){ ?int count=0; ?for(int i=0;i<s.length();i++){ ? if(s.charAt(i)==c){ ? ?count++; ? } ?} ?return count; } public static void main(String args[]){ ?System.out.println(new CharCounter().counter("LOVELOVEYOU",'O')); }}
舉報(bào)
課程升級(jí)!以終為始告別枯燥,在開(kāi)發(fā)和重構(gòu)中體會(huì)Java面向?qū)ο缶幊痰膴W妙
3 回答如何判斷一個(gè)字符串不存在一個(gè)數(shù)組當(dāng)中
1 回答定義一個(gè)用戶自定義輸入變量,如何輸出某個(gè)數(shù)組關(guān)于這個(gè)變量長(zhǎng)度的元素?
2 回答如何在一個(gè)java文件中,使用兩個(gè)同名的類(lèi)
1 回答如何用記事本編輯
2 回答Java中如何產(chǎn)生幾個(gè)互不相同的隨機(jī)數(shù)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2015-12-04
2015-12-04
2015-12-04
?System.out.println(new CharCounter().counter("LOVELOVEYOU",'O'));//這一行是什么意思?
2015-12-04
public class CharCounter{
public static int counter(String s,char c){
?int count=0;
?for(int i=0;i<s.length();i++){
? if(s.charAt(i)==c){
? ?count++;
? }
?}
?return count;
}
public static void main(String args[]){
?System.out.println(new CharCounter().counter("LOVELOVEYOU",'O'));
}
}