文本单词统计java代码 java统计某个单词出现的次数
java从键盘或文件读取一段英文文字,统计其中单词个数,并输出所有单词(忽略其他字
public void test(){
创新互联专注于江门网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供江门营销型网站建设,江门网站制作、江门网页设计、江门网站官网定制、小程序制作服务,打造江门网络公司原创品牌,更为您提供江门网站排名全网营销落地服务。
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入字符串...");
String str = sc.next();
char[] arr = str.toCharArray();
java.util.MapString, Integer map = new java.util.HashMapString, Integer();
for(int i = 0; i arr.length; i++){
int count = 0;
w:if(((int)arr[i] = 65 || (int)arr[i] = 90) ((int)arr[i] = 97) || ((int)arr[i] = 122)){
for(int j = 0; j arr.length; j++){
if(arr[i] == arr[j]){
++count;
}
}
map.put(arr[i]+"", count);
}
}
java.util.Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
Integer val = map.get(key);
System.out.println(key+"出现了:" + val);
}
System.out.println("输入的字符串为:"+java.util.Arrays.toString(arr));
}
怎么用java编写统计文件中的字符数、单词数和行数?
在C盘新建文件1.txt,输入任意字符,如下图:
编写java代码。如下:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeMap;
public class Test {
// 统计数字或者字符出现的次数
public static TreeMapCharacter, Integer Pross(String str) {
char[] charArray = str.toCharArray();
TreeMapCharacter, Integer tm = new TreeMapCharacter, Integer();
for (int x = 0; x charArray.length; x++) {
if (!tm.containsKey(charArray[x])) {
tm.put(charArray[x], 1);
} else {
int count = tm.get(charArray[x]) + 1;
tm.put(charArray[x], count);
}
}
return tm;
}
public static void main(String[] args) {
BufferedReader br = null;
int line = 0;
String str = "";
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new FileReader("c:\\1.txt"));
while ((str = br.readLine()) != null) {
sb.append(str);
++line;
}
System.out.println("\n文件行数: " + line);
System.out.println("\n文件内容: " + sb.toString());
TreeMapCharacter, Integer tm = Pross(sb.toString());
System.out.println("\n字符统计结果为:" + tm);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}运行结果如下图:
java中,给定一个文本,统计其中的单词个数,要求以单词在文本中出现的先后顺序输出
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordCount {
public static void main(String[] args) throws Exception {
File file = new File("1.txt");
PrintWriter pw = new PrintWriter(file);
pw.println("hello world, hello java.");
pw.println("hello program, hello java.");
pw.close();
printWords(file);
}
public static void printWords(File file) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(file));
Pattern pattern = Pattern.compile("[A-Za-z]+");
MapString, Integer map = new LinkedHashMap();
while (br.ready()) {
Matcher matcher = pattern.matcher(br.readLine());
while (matcher.find()) {
String word = matcher.group();
if (!map.containsKey(word)) {
map.put(word, 0);
}
map.put(word, map.get(word) + 1);
}
}
br.close();
for (String word : map.keySet()) {
System.out.println(word + " : " + map.get(word));
}
}
}
用java 编写一个程序,接受用户输入的一段英文文字,统计出其中的字符个数、单词个数和句子的个数。
public static void main(String[] args)
{
System.out.println("请输入英语片段,以';'结束:");
Scanner scanner = new Scanner(System.in);
String str = "";
int dc = 0;
int zc = 0;
int jc = 0;
while(scanner.hasNext())
{
str = scanner.next();
zc += str.length();
dc++;
System.out.println(str);
if (str.contains("."))
{
jc += 1;
}
if (str.contains(";"))
{
break;
}
}
// scanner.
System.out.println("单词个数为:" + dc);
System.out.println("字母数为:" + (zc - 1));
System.out.println("句数为:" + jc);
}
扩展资料:
import java.io.*;
public class test
{ public static void main(String[] args) throws IOException
{ System.out.print("请输入一串字母:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int char1=0,letter=0,sentence=0;
for(int i=0;istr.length();i++)
{
char ch = str.charAt(i);
if(ch!=' ')
{ char1++;
}
if(ch==' '||ch=='/n')
{ //有空格,加一单词
if(i!=0str.charAt(i-1)!=' ')
letter++;
}
if(ch=='?'||ch=='!'||ch=='.')
{ sentence++;
}
}
System.out.println("字符数:"+char1);
System.out.println("单词个数:"+letter);
System.out.println("句子数:"+sentence);
}
}
import java.io.*;
public class test
{ public static void main(String[] args) throws IOException
{ System.out.print("请输入一串字母:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int char1=0,letter=0,sentence=0;
for(int i=0;istr.length();i++)
{
char ch = str.charAt(i);
if(ch!=' ')
{ char1++;
}
if(ch==' '||ch=='/n')
{ //有空格,加一单词
if(i!=0str.charAt(i-1)!=' ')
letter++;
}
if(ch=='?'||ch=='!'||ch=='.')
{ sentence++;
}
}
System.out.println("字符数:"+char1);
System.out.println("单词个数:"+letter);
System.out.println("句子数:"+sentence);
}
}
文章标题:文本单词统计java代码 java统计某个单词出现的次数
网站网址:http://scyanting.com/article/dojeoeo.html