写一个java程序,数每个句子中每个单词出现的次数。
写一个java程序,数每个句子中每个单词出现的次数。举个例子:Designandcodeaprogramthatreadsatextfile,andcountsthenu...
写一个java程序,数每个句子中每个单词出现的次数。
举个例子:
Design and code a program that reads a text file, and counts the number of times each word that it contains appears.
输出:
design 1
and 2
code 1
a 2
program 1
that 2
reads 1
text 1
file 1
counts 1
the 1
number 1
of 1
times 1
each 1
word 1
it 1
contains1
appears 1
要求忽略大小写,标点另算。
问题补充:请各位java大侠帮忙,不胜感激!!!
要求文本在命令行中可以输入。。。 展开
举个例子:
Design and code a program that reads a text file, and counts the number of times each word that it contains appears.
输出:
design 1
and 2
code 1
a 2
program 1
that 2
reads 1
text 1
file 1
counts 1
the 1
number 1
of 1
times 1
each 1
word 1
it 1
contains1
appears 1
要求忽略大小写,标点另算。
问题补充:请各位java大侠帮忙,不胜感激!!!
要求文本在命令行中可以输入。。。 展开
2个回答
2013-09-20
展开全部
import java.util.*;
public class Test{
public static void main(String arg[]){
ArrayList<String> al=new ArrayList<String>();
Scanner s=new Scanner(System.in);
StringTokenizer st=new StringTokenizer(s.nextLine()," ");
while(st.hasMoreTokens()){
String ss=st.nextToken();
if(ss.charAt(ss.length()-1)>'z'||ss.charAt(ss.length()-1)<'A')
ss=ss.substring(0,ss.length()-1);
al.add(ss);
}
while(al.size()!=0){
String sl;
int count;
sl=al.remove(0);
count=1;
while(al.indexOf(sl)!=-1){
count++;
al.remove(sl);
}
System.out.println(sl+" "+count);
}
}
}
public class Test{
public static void main(String arg[]){
ArrayList<String> al=new ArrayList<String>();
Scanner s=new Scanner(System.in);
StringTokenizer st=new StringTokenizer(s.nextLine()," ");
while(st.hasMoreTokens()){
String ss=st.nextToken();
if(ss.charAt(ss.length()-1)>'z'||ss.charAt(ss.length()-1)<'A')
ss=ss.substring(0,ss.length()-1);
al.add(ss);
}
while(al.size()!=0){
String sl;
int count;
sl=al.remove(0);
count=1;
while(al.indexOf(sl)!=-1){
count++;
al.remove(sl);
}
System.out.println(sl+" "+count);
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-20
展开全部
大哥,这个问题主要是考察String类的方法使用
请你查看JDK API帮助文档java.lang.String
自己解决问题将受益匪浅
请你查看JDK API帮助文档java.lang.String
自己解决问题将受益匪浅
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询