第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

如何編寫(xiě)記錄一個(gè)文檔中某個(gè)字?jǐn)?shù)出現(xiàn)的次數(shù)

如何編寫(xiě)記錄一個(gè)文檔中某個(gè)字?jǐn)?shù)出現(xiàn)的次數(shù)

正在回答

4 回答

????????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();
		}
	}


1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

小豪呵呵 提問(wèn)者

大神,你好。希望能加上注釋?zhuān)矣行┛床惶浅8兄x!
2015-12-04 回復(fù) 有任何疑惑可以回復(fù)我~
#2

小豪呵呵 提問(wèn)者

非常感謝!
2015-12-04 回復(fù) 有任何疑惑可以回復(fù)我~
/*
	?*?讀取一個(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();
		}
	}


1 回復(fù) 有任何疑惑可以回復(fù)我~

?System.out.println(new CharCounter().counter("LOVELOVEYOU",'O'));//這一行是什么意思?

0 回復(fù) 有任何疑惑可以回復(fù)我~

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'));
}
}

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

小豪呵呵 提問(wèn)者

System.out.println(new CharCounter().counter("LOVELOVEYOU",'O'));//這一行是什么意思?
2015-12-04 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

如何編寫(xiě)記錄一個(gè)文檔中某個(gè)字?jǐn)?shù)出現(xiàn)的次數(shù)

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)