如何把字符串中重复的字符找出来?请教高人相助! 如果是统计单词的话,参考http://blog.csdn.net/treeroot/archive/2004/12/29/232797.aspx 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.util.*;import java.io.*;class Counter{ private int i = 1; int read(){ return i; } void increment(){ i++; }}public class WordCount{ private FileReader f; private TreeMap counts = new TreeMap(); private StreamTokenizer st; WordCount(String file) throws FileNotFoundException{ f = new FileReader(file); st = new StreamTokenizer( new BufferedReader(f)); } void countWords(){ try{ //st.ordinaryChar('e'); //st.eolIsSignificant(true); while(st.nextToken() != StreamTokenizer.TT_EOF){ String s; switch(st.ttype){ case StreamTokenizer. TT_EOL: s = "EOL"; break; case StreamTokenizer.TT_NUMBER: s = String.valueOf(st.nval); break; case StreamTokenizer.TT_WORD: s = st.sval;break; default: s = String.valueOf((char)st.ttype); } if (counts.containsKey(s)) ((Counter)counts.get(s)).increment(); else counts.put(s, new Counter()); } }catch(IOException e){ System.err.println("countWords() unsuccessful"); } } Collection values(){ return counts.values(); } Set keySet(){ return counts.keySet(); } Counter getCounter(String key){ return (Counter)counts.get(key); } void cleanup(){ try{ f.close(); }catch(IOException e){ System.err.println("f.close() unsuccessful"); } } public static void main(String[] args) throws FileNotFoundException{ WordCount wc = new WordCount("Test.txt"); wc.countWords(); Iterator it = wc.keySet().iterator(); String k; while(it.hasNext()){ k = (String)it.next(); System.out.println(k + " : " + wc.getCounter(k).read()); } } } IT|06年3G手机走进生活|搜狐数码|市场热点:全面解读数码相机今年的四大焦点|搜狐IT|将建三张3G网?|搜狐数码|宽屏手机成新热点|搜狐数码|最新宽屏手机逐个数|搜狐数码|最新宽屏手机逐个数|新浪上面是我所说的TXT文件里的内容,我想把这里面有“最新宽屏手机逐个数”是重复的。把它重复的个数取出来。并把“数码|最新宽屏手机逐个数|搜狐”放在最前面。谢谢大家的支持!感谢! 阿訇哈。。强盗。。我当小偷跟楼主一起学习import java.util.*;import java.io.*;class Counter{ private int i = 1; int read(){ return i; } void increment(){ i++; }}public class WordCount{ private FileReader f; private TreeMap counts = new TreeMap(); private StreamTokenizer st; WordCount(String file) throws FileNotFoundException{ f = new FileReader(file); st = new StreamTokenizer( new BufferedReader(f)); } void countWords(){ try{ //st.ordinaryChar('e'); //st.eolIsSignificant(true); while(st.nextToken() != StreamTokenizer.TT_EOF){ String s; switch(st.ttype){ case StreamTokenizer. TT_EOL: s = "EOL"; break; case StreamTokenizer.TT_NUMBER: s = String.valueOf(st.nval); break; case StreamTokenizer.TT_WORD: s = st.sval;break; default: s = String.valueOf((char)st.ttype); } if (counts.containsKey(s)) ((Counter)counts.get(s)).increment(); else counts.put(s, new Counter()); } }catch(IOException e){ System.err.println("countWords() unsuccessful"); } } Collection values(){ return counts.values(); } Set keySet(){ return counts.keySet(); } Counter getCounter(String key){ return (Counter)counts.get(key); } void cleanup(){ try{ f.close(); }catch(IOException e){ System.err.println("f.close() unsuccessful"); } } public static void main(String[] args) throws FileNotFoundException{ WordCount wc = new WordCount("Test.txt"); wc.countWords(); Iterator it = wc.keySet().iterator(); String k; while(it.hasNext()){ k = (String)it.next(); System.out.println(k + " : " + wc.getCounter(k).read()); } } } 反编译后内部类的问题 java静态方面的问题 Lucene怎么发音? 如何在Java的Label中显示多行文字? 学JAVA选JB好还是VJ#好?请资深的JAVA程序员给点意见。谢谢!! 请问如何设置弹出菜单的大小,我想把菜单项默认的高度改小点 请问从哪里可以下载FtpClient这个类 用javamail发送附件名为中文名时的一个问题,送分45!!!! Swing 高手这边看! c3p0---MYSQL驱动 #####做了个和QQ、MSN有点像的东东####### 取applet窗口中文本筐的值(有趣而有难度!!)
import java.io.*;class Counter{
private int i = 1;
int read(){
return i;
}
void increment(){
i++;
}
}public class WordCount{
private FileReader f;
private TreeMap counts = new TreeMap();
private StreamTokenizer st;
WordCount(String file) throws FileNotFoundException{
f = new FileReader(file);
st = new StreamTokenizer(
new BufferedReader(f));
}
void countWords(){
try{
//st.ordinaryChar('e');
//st.eolIsSignificant(true);
while(st.nextToken() != StreamTokenizer.TT_EOF){
String s;
switch(st.ttype){
case StreamTokenizer.
TT_EOL: s = "EOL";
break;
case StreamTokenizer.TT_NUMBER:
s = String.valueOf(st.nval);
break;
case StreamTokenizer.TT_WORD:
s = st.sval;break;
default:
s = String.valueOf((char)st.ttype);
}
if (counts.containsKey(s))
((Counter)counts.get(s)).increment();
else
counts.put(s, new Counter());
}
}catch(IOException e){
System.err.println("countWords() unsuccessful");
}
}
Collection values(){
return counts.values();
}
Set keySet(){
return counts.keySet();
}
Counter getCounter(String key){
return (Counter)counts.get(key);
}
void cleanup(){
try{
f.close();
}catch(IOException e){
System.err.println("f.close() unsuccessful");
}
}
public static void main(String[] args) throws FileNotFoundException{
WordCount wc = new WordCount("Test.txt");
wc.countWords();
Iterator it = wc.keySet().iterator();
String k;
while(it.hasNext()){
k = (String)it.next();
System.out.println(k + " : " +
wc.getCounter(k).read());
}
}
}
数码|市场热点:全面解读数码相机今年的四大焦点|搜狐
IT|将建三张3G网?|搜狐
数码|宽屏手机成新热点|搜狐
数码|最新宽屏手机逐个数|搜狐
数码|最新宽屏手机逐个数|新浪上面是我所说的TXT文件里的内容,我想把这里面有“最新宽屏手机逐个数”是重复的。把它重复的个数取出来。并把“数码|最新宽屏手机逐个数|搜狐
”放在最前面。谢谢大家的支持!感谢!
强盗。。
我当小偷
跟楼主一起学习
import java.util.*;
import java.io.*;class Counter{
private int i = 1;
int read(){
return i;
}
void increment(){
i++;
}
}public class WordCount{
private FileReader f;
private TreeMap counts = new TreeMap();
private StreamTokenizer st;
WordCount(String file) throws FileNotFoundException{
f = new FileReader(file);
st = new StreamTokenizer(
new BufferedReader(f));
}
void countWords(){
try{
//st.ordinaryChar('e');
//st.eolIsSignificant(true);
while(st.nextToken() != StreamTokenizer.TT_EOF){
String s;
switch(st.ttype){
case StreamTokenizer.
TT_EOL: s = "EOL";
break;
case StreamTokenizer.TT_NUMBER:
s = String.valueOf(st.nval);
break;
case StreamTokenizer.TT_WORD:
s = st.sval;break;
default:
s = String.valueOf((char)st.ttype);
}
if (counts.containsKey(s))
((Counter)counts.get(s)).increment();
else
counts.put(s, new Counter());
}
}catch(IOException e){
System.err.println("countWords() unsuccessful");
}
}
Collection values(){
return counts.values();
}
Set keySet(){
return counts.keySet();
}
Counter getCounter(String key){
return (Counter)counts.get(key);
}
void cleanup(){
try{
f.close();
}catch(IOException e){
System.err.println("f.close() unsuccessful");
}
}
public static void main(String[] args) throws FileNotFoundException{
WordCount wc = new WordCount("Test.txt");
wc.countWords();
Iterator it = wc.keySet().iterator();
String k;
while(it.hasNext()){
k = (String)it.next();
System.out.println(k + " : " +
wc.getCounter(k).read());
}
}
}