免責聲明:此問題屬於作業。我一直在嘗試這一點,我已經拿出了我嘗試過的東西,因爲它只是變得冗餘。我的問題是如何計算我的文件中「非線性」的數量字符。我找到了計算有多少非ASCII字符出現的方法。儘管如此,這條路線仍在困擾着我。計算具有非asscii字符的文件的行數
例如,如果文件中的一行代碼是èèèèè,那麼movieCount應該增加1,我的ascCount應該增加5.不是所有行都會有非ascii字符。
public static void main(String [] args) throws FileNotFoundException{
//open the file
File movieFile = new File("/home/turing/t90rkf1/d470/dhw/hw5-movies/movie-names.txt");
InputStream file = new FileInputStream(movieFile);
String empty = null;
int movieCount = 0;
int ascCount = 0;
try {
FileReader readFile = new FileReader(movieFile);
BufferedReader buffMovie = new BufferedReader(readFile);
//read while stream is not empty
while ((empty = buffMovie.readLine()) != null){
//check the value for ascii
for(int j = 0, n = empty.length(); j < n; j++){
char asc = empty.charAt(j);
if(asc > 127){
ascCount++;
}
}
}
您正在增加ascCount而不是movieCount。 – SachinSarawgi
也考慮使用正則表達式來定位非ASCII字符http://stackoverflow.com/questions/2124010/grep-regex-to-match-non-ascii-characters –
爲什麼你要計算非ASCII字符? –