一个作业:从一篇文章中取单词,并按字母顺序进行了排列(已经实现)
现在根据这组单词,分别列出他们所在的行 ,比如 like {1,6,26}
不是这一个单词,是这一组已经排序的单词都要找出来,这个如何去实现呢?
我这个方法已经可以实现找一个单词,但是无法按排序的单词组,按从小到大的顺序进行找。我贴了方法,帮我看看吧,裸身跪求了!!! public String[] lecteur (String file) {
List<String> liste1 = new Vector<String>();
List<String> liste2 = new Vector<String>();
String chbuf;
String table[]=new String[3000];
//for( motListe1=0;motListe1<liste1.size();motListe1++)
try { File f1 = new File(file);
Scanner sc1 = new Scanner(f1,"ISO-8859-1");
while (sc1.hasNext()) {
chbuf = nettoyage(sc1.next()) ;
if (!chbuf.equals("")){
ajouter_elt(chbuf, liste1);
}
Collections.sort(liste1); //par ordre
}
for(int i=0;i<liste1.size();i++) //create la table
{table[i]=liste1.get(i); //retire le mot dans la liste
//System.out.println(i+": "+table[i]);
}// *****************************************************************
File f2 = new File(file);
Scanner sc2 = new Scanner(f2,"ISO-8859-1");
while (sc2.hasNextLine()) {
numbreLine++;
chbuf = nettoyage(sc2.nextLine()) ;
if (!chbuf.equals("")){
ajouter_elt(chbuf, liste2);
}
String[] word = chbuf.split(" ");
Pattern p1=Pattern.compile("[a-zA-Zé\\à\\ê\\è]{4,20}");
for(int j=0;j<word.length;j++){
Matcher matcher1=p1.matcher(word[j]);
while(matcher1.find())
m.put((String)matcher1.group(),0);
} Set<String> set = m.keySet();
for(int i=0;i<word.length;i++) {
Matcher matcher1=p1.matcher(word[i]); Iterator<String> it = set.iterator();
while(matcher1.find()){
String wordNew=matcher1.group(); while(it.hasNext()){
String k = nettoyage(it.next()); if(wordNew.equals(k)){ int c = m.get(k);
c++;
m.put(nettoyage(wordNew),c);
}
//******************************************************************************
if(liste1.get(l).equals(wordNew)){
int c = 0;
c++;
m2.put(numbreLine,c);
}
}
}
} m.clear();
} for(int line=0;line<liste1.size();line++)
System.out.println(liste1.get(line)+":"+m2); } catch (FileNotFoundException ex) {
Logger.getLogger(Question3_Refaire_4.class.getName()).log(Level.SEVERE, null, ex);
}
Collections.sort(liste2);
return liste2.toArray(new String[0]);
}
下面还有自己写的方法,这只是部分,谢谢了!!!
现在根据这组单词,分别列出他们所在的行 ,比如 like {1,6,26}
不是这一个单词,是这一组已经排序的单词都要找出来,这个如何去实现呢?
我这个方法已经可以实现找一个单词,但是无法按排序的单词组,按从小到大的顺序进行找。我贴了方法,帮我看看吧,裸身跪求了!!! public String[] lecteur (String file) {
List<String> liste1 = new Vector<String>();
List<String> liste2 = new Vector<String>();
String chbuf;
String table[]=new String[3000];
//for( motListe1=0;motListe1<liste1.size();motListe1++)
try { File f1 = new File(file);
Scanner sc1 = new Scanner(f1,"ISO-8859-1");
while (sc1.hasNext()) {
chbuf = nettoyage(sc1.next()) ;
if (!chbuf.equals("")){
ajouter_elt(chbuf, liste1);
}
Collections.sort(liste1); //par ordre
}
for(int i=0;i<liste1.size();i++) //create la table
{table[i]=liste1.get(i); //retire le mot dans la liste
//System.out.println(i+": "+table[i]);
}// *****************************************************************
File f2 = new File(file);
Scanner sc2 = new Scanner(f2,"ISO-8859-1");
while (sc2.hasNextLine()) {
numbreLine++;
chbuf = nettoyage(sc2.nextLine()) ;
if (!chbuf.equals("")){
ajouter_elt(chbuf, liste2);
}
String[] word = chbuf.split(" ");
Pattern p1=Pattern.compile("[a-zA-Zé\\à\\ê\\è]{4,20}");
for(int j=0;j<word.length;j++){
Matcher matcher1=p1.matcher(word[j]);
while(matcher1.find())
m.put((String)matcher1.group(),0);
} Set<String> set = m.keySet();
for(int i=0;i<word.length;i++) {
Matcher matcher1=p1.matcher(word[i]); Iterator<String> it = set.iterator();
while(matcher1.find()){
String wordNew=matcher1.group(); while(it.hasNext()){
String k = nettoyage(it.next()); if(wordNew.equals(k)){ int c = m.get(k);
c++;
m.put(nettoyage(wordNew),c);
}
//******************************************************************************
if(liste1.get(l).equals(wordNew)){
int c = 0;
c++;
m2.put(numbreLine,c);
}
}
}
} m.clear();
} for(int line=0;line<liste1.size();line++)
System.out.println(liste1.get(line)+":"+m2); } catch (FileNotFoundException ex) {
Logger.getLogger(Question3_Refaire_4.class.getName()).log(Level.SEVERE, null, ex);
}
Collections.sort(liste2);
return liste2.toArray(new String[0]);
}
下面还有自己写的方法,这只是部分,谢谢了!!!
解决方案 »
- java基础while循环计算
- java 中Graphics 颜色设置问题
- 给个思想,和建议投骰子游戏应用程序
- 一个菜问:刚学做鼠标动作。
- 高分!!如何改进我的软件实现中英文界面互换呀?小女先谢过啦!
- 我在run方法里面加入一个设置守护线程的语句 为何报错
- JBuilder中如何支持鼠标滚轮??(急!!)
- 谁知道那有免费的dBase数据的jdbc驱动下载?也就是读dbf文件的?
- 关于tomcat设置的问题简单
- 为什么ArrayList直接输出不是一个内存地址,而是数组元素(toString输出)
- 关于JRE的精简??
- 做了个培训方面的门户网站,有个业内人看了,就说技术陈旧!哎~~~~希望大家看看我这样做是否真的陈旧。如果真的陈旧,需要在那里改善?555555
int c = 0;
c++;
m2.put(numbreLine,c);
}
比较就是这一段,liste1是已经排序的单词组,wordNew我是按行找的单词
用liste1的单词,按行进行和wordNew比较,然后如果有相同就输出行到m2(hashMap)中
这是这个程序的用意,但我只能找一个单词比如liste1.get(1),我现在要把1变为动态的变量
变成liste1.size(),可以这样一来,程序永远只能找第一个liste1.get(0),始终无法自增大
题目应该说清楚了,大家帮我看看,问题出在哪里,裸身跪谢
int c = 0;
c++;
m2.put(numbreLine,c);
} map.put(wordNew,map.get(wordNew)+" "+numberLine);
还有 我也很纳闷 既然可以找到list(0) 怎么可能找不到后面的呢 纳闷....