Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java计算X字母单词的频率/发生率?_Java - Fatal编程技术网

Java计算X字母单词的频率/发生率?

Java计算X字母单词的频率/发生率?,java,Java,很抱歉我不得不问这个问题,我真诚地感谢你的帮助。我正在使用Java,我需要计算x字母单词出现的次数。单词x基本上是一个数字,用任何数字(即1/2/3)替换它;) x字母单词=1个字母单词/2个字母单词/3个字母单词,依此类推 因此,如果文本文件包含以下文本: Hello World! This is a sample file. 应从Java输出以下内容: 1个字母单词:1 2个字母单词:2 3个字母单词:2 4个字母单词:3 5个字母单词:0 .. 9个字母单词:1 很抱歉给你添麻烦,但这可

很抱歉我不得不问这个问题,我真诚地感谢你的帮助。我正在使用Java,我需要计算x字母单词出现的次数。单词x基本上是一个数字,用任何数字(即1/2/3)替换它;)

x字母单词=1个字母单词/2个字母单词/3个字母单词,依此类推

因此,如果文本文件包含以下文本:

Hello World! This is a sample file.
应从Java输出以下内容:

1个字母单词:1 2个字母单词:2 3个字母单词:2 4个字母单词:3 5个字母单词:0 .. 9个字母单词:1

很抱歉给你添麻烦,但这可能吗?如果没有任何意义,请告诉我。我衷心感谢您的帮助!:D

非常感谢你

那么:

String string = "Hello World! This is a sample file.";
String[] strings = string.split(" ");
int[] counts = new int[30]; //Change this depending on how far you want to go
for(String str : strings)
     if(str.length() < counts.length) counts[str.length()] += 1;
for(int i = 1; i < counts.length; i++)
    System.out.println(i + " letter words: " + counts[i]);
String=“你好,世界!这是一个示例文件。”;
String[]strings=String.split(“”);
int[]计数=新的int[30]//根据您要走多远更改此选项
for(字符串str:strings)
如果(str.length()

非常基本,根据需要进行修改。

在提问之前,您需要进行谷歌搜索并查看其他地方,我们通常会处理一些小问题或错误的代码修复。然而,由于人们可能会来寻找现在谷歌可以找到这个页面

首先要读取一个文件,您需要一些流,这些流可以对数据进行输入/输出

File file = new File("C:\\MyFile.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
您将使用末端的一个来访问您的数据。如果文件包含多行,则需要使用循环获取所有行:

while (dis.available() != 0) { // Tests for more lines
    String s = dis.readLine(); // Get the line
现在,使用sushain97显示的方法将行拆分为不同的字符串:

String[] sParts = s.split(" "); // Splits the strings into an array using the 'SPACE' as a reference
我们需要所有字母的整数数组。长度将是最长的字符串

int longestLength = 1; // The longest length of word
for (String strx : sParts) { // Go through each sPart, the next one is called strx
    if (longestLength < strx.length()) // If it's got a longer length than the current record
        longestLength = strx.length(); // Set a new record
}
int[] counts = new int[longestLength + 1]; // Make the array with the length needed; +1 since arrays are 0-based
并将其输出:

for(int i = 1; i < counts.length; i++) // We use this type of loop since we need the length
    System.out.println(i + " letter words: " + counts[i]);
} // Close that while loop from before
for(int i=1;i

今后,请按照我的建议去做,但我希望这能帮助所有来这里获取信息的谷歌人

到目前为止,你试过什么?提供代码并说明问题所在。x字母单词出现的次数,你所说的
x字母
到底是什么意思,因为我在你发布的文本中没有看到任何
x
。我现在拥有的代码几乎什么都没有。目前它所做的只是缓冲一个txt文件,就是这样。我似乎什么也做不了(我已经试过了:和@AUllah1-基本上,您要求我们为您编写代码。这是不可接受的。Stephen C抱歉,我将发布我当前的代码。:)非常感谢!我已经做了好几个小时了,哈哈。非常感谢!:)@AUllah1没有问题,如果要删除非字母/空格字符:
string=string.replaceAll(“[^a-zA-Z]”,则“)
for(int i = 1; i < counts.length; i++) // We use this type of loop since we need the length
    System.out.println(i + " letter words: " + counts[i]);
} // Close that while loop from before