te.java

来自「计算文本内所有字符重复数的算法」· Java 代码 · 共 69 行

JAVA
69
字号
import java.io.*; 

public class te{ 
private String words; 
public te(String input)
{ 
words = input.toString();; 
} 
public int getNum()//return the lenth of the .txt file
{ 
return words.length(); 
} 
public int result(char c)//calculate how many times the character c show up
{ 
String temp=new String(words); 
int index=temp.indexOf(c); 
int n=0; 
while(index>=0) 
{ 
temp=temp.substring(index+1,temp.length()); 
index=temp.indexOf(c); 
n++; 
} 
return n; 
} 

public static void main(String[] args)throws IOException{ 
BufferedReader buf;//file place 
buf=new BufferedReader(new InputStreamReader(System.in)); 
String fileinput; 
System.out.println("Please input the place where your file is:");; 
String strtotal=""; 
try{ 
fileinput=buf.readLine(); 
if(fileinput.equals("quit"))System.exit(0); 

System.out.println(); 

File file=new File(fileinput); 
FileReader fr=new FileReader(file); 
LineNumberReader in=new LineNumberReader(fr); 
int n=(int)file.length(); 
in.mark(n); 
in.skip(n); 
int k=in.getLineNumber(); 
in.reset(); 
String str; 

while((str=in.readLine())!=null){ 
System.out.println(str); 
strtotal+=str; 
} 
in.close(); 
}catch(IOException e){ 
System.out.println(e); 
} //get the file's words

te word=new te(strtotal);
int[] num = new int[150]; //define the num to load characters
int i; 
char ch='A'; 
for (i=0;ch<150;ch++,i++) //calculate the times of every characters show up
num[i]=word.result(ch); 
System.out.println("The number of the characters is : "+word.getNum()); //output the length
for (i=0,ch='A';i<70;ch++,i++) //output the letters and their times
System.out.println("The num "+ch+" has presented : "+num[i]); 

} 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?