网站建设资讯

NEWS

网站建设资讯

JAVA文本字符统计代码,java文本字符统计代码有哪些

利用Java语言代码输入一行字符分别统计其中英文字母、空格、数字和其他字符的个数。

123456789101112131415161718192021222324

创新互联建站专注于建始网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供建始营销型网站建设,建始网站制作、建始网页设计、建始网站官网定制、小程序开发服务,打造建始网络公司原创品牌,更为您提供建始网站排名全网营销落地服务。

public static void main(String[] args) throws IOException {        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));        String str=br.readLine();         int countNum = 0;//统计数字的个数        int countChar = 0;//统计英文字母的个数        int countSpace = 0;//统计空格的个数        int countOthers = 0;//统计其它字符的个数        for (int i = 0; i  str.length(); i++) {            char c = str.charAt(i);            if (c = '0'  (int) c = '9') {                countNum++;            } else if ((c = 'a'  c = 'z')||(c = 'A'  c = 'Z')) {                countChar++;            } else if (c == ' ') {                countSpace++;            } else{                countOthers++;            }        }        System.out.println("数字个数:"+countNum);        System.out.println("英文字母个数:"+countChar);        System.out.println("空格个数:"+countSpace);        System.out.println("其他字符个数:"+countOthers);    }

一、问题分析:

输入一行字母,那么会以换行结束。所以可以存入数组,也可以逐个输入,遇到换行结束。

要统计各个类的个数,就要逐个判断是哪个分类的。

由于在ASCII码中,数字,大写字母,小写字母分别连续,所以可以根据边界值判断类型。

二、算法设计:

1、读入字符,直到遇到换行结束。

2、对于每个字符,判断是字母还是数字,或者空格,或者是其它字符。

3、对于每个字符判断后,对应类别计数器自加。

4、最终输出结果。

java统计串中字符个数的代码解释

整个代码就是为了统计字符串中各个字符出现的个数

Map

result

=

getCharMaps(s);

这个方法调用

主要是看这个方法

public

static

Map

getCharMaps(String

s)

{

Map

map

=

new

HashMap();

//定义一个map

for(int

i

=

0;

i

s.length();

i++)

{

//

下面的代码主要是循环字符串,一次取出每一个字符

Character

c

=

s.charAt(i);

//获取对应的字符

Integer

count

=

map.get(c);

//获取字符对应出现的次数

map.put(c,

count

==

null

?

1

:

count

+

1);

//如果之前出现过,则加1,否则直接赋1

}

return

map;

//返回最终的map

}

用JAVA编写一个程序,统计一个文本文件中字符A的个数

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class Count {

public static void main(String[] args) {

System.out.println("Please input the path of the file:");

Scanner scanner = new Scanner(System.in);

String path = scanner.nextLine();

scanner.close();

File file = new File(path);

char des = 'A';

int count = process(file, des);

System.out.println("字符" + des + "在文中出现的次数为:" + count);

}

public static int process(File file, char c) {

int count = 0;

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(file));

String temp = "";

while ((temp = br.readLine()) != null) {

char[] des = temp.trim().toCharArray();

for (int i = 0; i  des.length; i++) {

if (des[i] == c){

count++;

}

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return count;

}

}

测试文件:

运行截图:

PS:代码对于题中文件和查找字符已经单独封装成一个方法,可以灵活改动所需要查找的文件和查找的字符。

编写java程序统计字符个数。

public static void main(String[] args) {

// TODO Auto-generated method stub

int abcCount=0;//英文字母个数

int spaceCount=0;//空格键个数

int numCount=0;//数字个数

int otherCount=0;//其他字符个数

Scanner scan=new Scanner(System.in);

String str=scan.nextLine();

char[] ch = str.toCharArray();

for(int i=0;ich.length;i++){

if(Character.isLetter(ch[i])){

//判断是否字母

abcCount++;

}

else if(Character.isDigit(ch[i])){

//判断是否数字

numCount++;

}

else if(Character.isSpaceChar(ch[i])){

//判断是否空格键

spaceCount++;

}

else{

//以上都不是则认为是其他字符

otherCount++;

}

}

System.out.println("字母个数:"+abcCount);

System.out.println("数字个数:"+numCount);

System.out.println("空格个数:"+spaceCount);

System.out.println("其他字符个数:"+otherCount);

求大神教我,java语言里的字数统计功能的代码?

其实直接把要统计的字数放在一个字符串里就行了string str = "";

然后再用string的length方法就可以统计字符串长度了。

string str = "xxxxx";

int strlen = str.length();

system.out.print(strlen);

打印出来的数字就是所输入的字数(这里是包括标点符号的,如果要去标点空格可以用正则表达式)


文章题目:JAVA文本字符统计代码,java文本字符统计代码有哪些
分享URL:http://cdweb.net/article/hschee.html