一个作业:从一篇文章中取单词,并按字母顺序进行了排列(已经实现)
现在根据这组单词,分别列出他们所在的行 ,比如 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]);
}
下面还有自己写的方法,这只是部分,谢谢了!!!
解决方案 »
- 关于AS3 二维数组定义问题。【大家帮忙看下】
- 多线程并发问题
- 关于输入输出
- 跪求一个程序,急
- 再提供30分,寻求答案!!(共计130分)
- 请问一个链表删除节点的问题
- 请帮忙看一下这段代码!有Notes消息。
- 关于中文版jdk的一点想法
- 我和我的朋友机器联网了,我的我的java程序(用jdbc驱动)可以连到他的sqlserver.但不可以连到我自己的。但我的java程序(用jdbc-odbc桥)连
- JTable怎么不显示栏目的名称了?
- 关于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) 怎么可能找不到后面的呢 纳闷....