关于文本字符统计java实验的信息

博主:adminadmin 2022-11-23 18:24:09 54

本篇文章给大家谈谈文本字符统计java实验,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用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中统计文本字符串的个数怎么做

可以用java.util.StringTokenizer类,

StringTokenizer st = new StringTokenizer("this is a test");

System.out.println("字符串个数:" + st.countTokens());

用java统计文本中相同字符出现的次数

通过循环遍历字符串,然后一个个的比较,记下相同字符的个数就行了。代码如下:

import java.util.Scanner;

import java.util.TreeMap;

/**

 * 从键盘输入16位长整数,编程统计每个数字出现的个数

 * @author young

 *

 */

public class CharMapDemo {

// 统计数字或者字符出现的次数

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) {

Scanner sc = new Scanner(System.in);

// System.out.println("请输入一个长整数:");

// int temp = sc.nextInt();

// String str = String.valueOf(temp);

// TreeMapCharacter, Integer tm = Pross(str);

// System.out.println(tm);

System.out.println("请输入一个字符串:");

String str = sc.nextLine();

TreeMapCharacter, Integer tm = Pross(str);

System.out.println(tm);

}

}

java怎么实现统计一个字符串中字符出现的次数?

这是我刚根据你的需求写的,你可以参考一下!

我办法很简单,拆分存放数组里面,然后查找数组就可以了!

其实还有更简单的办法,直接用容器比较方便,目前这个办法我自己用数组写的,我个人挺满意的!

import java.util.Scanner;

public class 统计次数 

{

private static Scanner sc=new Scanner(System.in);

public static void main(String[] args) 

{

System.out.println("\n\t\t==========统计字符串中字符次数==========\n");

init();

}//初始化!

private static void init()

{

for (; ; )

{

System.out.println("输入文字:");

char[] arr1=fenJie(input());

System.out.println("查找的字:");

char x=muBiao(fenJie(input()));

int y=chaZhao(arr1,x);

System.out.println("你要找的是\""+x+"\",出现"+y+"次\n===============\n");

}

}

//录入!

private static String input()

{

String s=sc.next();

return s;

}

//分解!

private static char[] fenJie(String s)

{

char[] arr=new char[s.length()];

for (int i=0;is.length() ;i++ )

{

arr[i]=(char)(s.charAt(i));

}

return arr;

}

//查找!

private static int chaZhao(char[] arr,char x)

{

int num=0,tem=-1;

for (int a=0,b=arr.length-1;a=b ;a++,b-- )

{

if(a!=barr[a]==x)num++;

if(a!=barr[b]==x)num++;

if((a==b)(arr[a]==x||arr[b]==x))num++;

tem=num;

}

return tem;

}

//目标

private static char muBiao(char[] arr)

{

return arr[0];

}

}

java统计文本文件中的字母、数字、汉字和符号

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class TestForOne {

public static void main(String[] args) {

String s=GetFiles("d:\\111.txt","",1);

WriteFiles(s);

}

public static String GetFiles(String BasePath,String Content, int f) {

/*

* 读写文件操作

*/

String s, s2 = new String();

try {

if (f == 1) {

BufferedReader in = new BufferedReader(new FileReader(BasePath));

while ((s = in.readLine()) != null) {

s2 += s + "\n";

}

in.close();

}

if(f==2)

{

PrintWriter out1=new PrintWriter(new BufferedWriter(new FileWriter(BasePath)));

out1.println(Content);

out1.close();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

return s2;

}

}

public static void WriteFiles(String s)

{

/*

* 对目标内容进行 判断 然后分别写入对应的 文件中

*/

String zimu="",shuzi="",hanzi="",qita="";

for(int i=0;is.length();i++)

{

if( ((s.charAt(i))='A' (s.charAt(i))='Z') || ((s.charAt(i))='a' (s.charAt(i))='z') )

{

zimu+=s.charAt(i);

continue;

}

if(s.charAt(i)='0' s.charAt(i)='9')

{

shuzi+=s.charAt(i);

continue;

}

if(s.charAt(i)=0x0391 s.charAt(i)=0xFFE5)

{

hanzi+=s.charAt(i);

continue;

}

else

{

qita+=s.charAt(i);

continue;

}

}

String content[]={zimu,shuzi,hanzi,qita};

String temp[]={"d:\\zimu.txt","d:\\shuzi.txt","d:\\hanzi.txt","d:\\qita.txt"};

for(int i=0;icontent.length;i++)

{

GetFiles(temp[i],content[i],2);

}

}

}

编写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实验和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。