免责声明:此问题属于作业。我一直在尝试这一点,我已经拿出了我尝试过的东西,因为它只是变得冗余。我的问题是如何计算我的文件中“非线性”的数量字符。我找到了计算有多少非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字符? –