如何计算文件中单词的长度?JAVA
我正在尝试编写一个代码,该代码将计算文件中特定长度的字数 例如:如何计算文件中单词的长度?JAVA,java,string,Java,String,我正在尝试编写一个代码,该代码将计算文件中特定长度的字数 例如: How are you? 将打印: Proportion of 3-letter words: 100% (3 words) 我想数一数长度为1,2,3,4,5,6,7,8,9,10,11,12,13的单词+ 你能指引我吗 我并不是在寻找字数。我已经能够使用此代码: public static int WordCount() throws FileNotFoundException { File file = new
How are you?
将打印:
Proportion of 3-letter words: 100% (3 words)
我想数一数长度为1,2,3,4,5,6,7,8,9,10,11,12,13的单词+
你能指引我吗
我并不是在寻找字数。我已经能够使用此代码:
public static int WordCount() throws FileNotFoundException
{
File file = new File("sample.txt");
Scanner keyboard = new Scanner(new FileInputStream(file));
int count=0;
while(keyboard.hasNext())
{
keyboard.next();
count++;
}
return count;
}
我想找到一定长度的单词
更新
我编写了以下代码:
public static int WordLengthCount() throws FileNotFoundException
{
File file = new File("hello.txt");
Scanner keyboard = new Scanner(new FileInputStream(file));
int count5 = 0;
int hell = 0; //This is just for the else command to compile
while(keyboard.hasNext())
{
if ( keyboard.next().length() == 5 )
{
count5++;
keyboard.next();
return count5;
}
} return hell;
}
您可以使用该方法计算字符串(word)中的字符数。从那以后,只需要把它保存在某个地方。例如,在地图中
:
public static Map<Integer, Integer> lengthCounts() throws FileNotFoundException
Map<Integer, Integer> countMap = new HashMap<>();
while(keyboard.hasNext())
{
String word = keyboard.next();
int length = word.length();
Integer currCount = countMap.get(length);
if (currCount == null) {
countMap.put (length, 1);
else {
countMap.put (length, currCount + 1);
}
}
return countMap;
}
因此,您的最终输出将是带有一个字母字符串计数、两个字母字符串计数等的map。其他答案非常好,但是如果您试图在文件中查找特定长度的单词,并且不喜欢上面的答案,那么您也可以尝试REGEX。你可以测试每个单词,然后用它做你想做的事情。如果要在每个长度的文件中查找单词数,我认为上面的答案更好,但是如果要检测特定长度的单词,可以使用.length()或下面的正则表达式。我认为使用strings.lenght()函数更好,但我只是给你一个替代答案和示例 我将在下面举一个小例子
public class Words{
public static void main(String [] args){
String [] words = {"Pizzaaa", "Pizza", "Party"};
int fives = 0;
for( String s : words){
if(s.matches(".{5}")){
5++;
}
}
System.out.println(fives);
}
}
或更好的版本:
public class Words{
public static void main(String [] args){
String [] words = {"Pizzaaa", "Pizza", "Party"};
int fives = 0;
for( String s : words){
if(s.length() == 5){
5++;
}
}
System.out.println(fives);
}
}
编辑如下:演示如何在基于文件的循环中使用它
// other code needed
while(in.hasNext())
{
String s = in.next();
if(s.length() == 5)
fives++;
}
例如,我在
C:\
有一个名为TextFile.txt
的文本文件,其内容如下:
Ut porttitor libero sodales quam sagittis, id facilisis lectus semper.
和Java代码:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
File file = new File("C:\\TextFile.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
if (dis.available() != 0) {
// Get the line.
String s = dis.readLine();
// Put words to array.
String[] sParts = s.split(" ");
// Initialize word longest length.
int longestLength = 1;
for (String strx : sParts) { // Go through each sPart, the next one is called strx
// If the document has word longer than.
if (longestLength < strx.length())
// Set new value for longest length.
longestLength = strx.length();
}
// Because array index from "0".
int[] counts = new int[longestLength + 1];
for (String str : sParts) {
// Add one to the number of words that length has
counts[str.length()] += 1;
}
// We use this type of loop since we need the length.
for (int i = 1; i < counts.length; i++) {
System.out.println(i + " letter words: " + counts[i]);
}
}
}
}
// Result:
// 1 letter words: 0
// 2 letter words: 2
// 3 letter words: 0
// 4 letter words: 1
// 5 letter words: 0
// 6 letter words: 2
// 7 letter words: 2
// 8 letter words: 0
// 9 letter words: 3
import java.io.BufferedInputStream;
导入java.io.DataInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
公开课范例{
公共静态void main(字符串[]args)引发IOException{
File File=新文件(“C:\\TextFile.txt”);
FileInputStream fis=新的FileInputStream(文件);
BufferedInputStream bis=新的BufferedInputStream(fis);
DataInputStream dis=新的DataInputStream(bis);
如果(dis.available()!=0){
//接电话。
字符串s=dis.readLine();
//将单词放入数组。
字符串[]sParts=s.split(“”);
//初始化单词的最长长度。
int longestLength=1;
对于(字符串strx:sParts){//遍历每个sParts,下一个称为strx
//如果文档的字长超过。
if(最长长度
显示您到目前为止尝试过的内容?可能重复@VictorSmt否,我不想找到所有单词,我想找到一定长度的单词。@Shriram我编辑了我的文章。@Saadat您可以将单词添加到列表中,然后按长度排序这有没有一种不映射的方法?我会更新我的问题,请你检查一下。谢谢我用更新编辑了这个问题,你能检查一下吗?@Saadat我想我现在更能理解你的问题了-请看我编辑的答案。谢谢。解决了我的问题:)我用Update编辑了这个问题,你能检查一下吗?嗨,我怎么能用这个文件?@Saadat只需使用if语句并将它插入到你的代码中。将if(s.lenth()==5)放入hasNext()的循环中
Ut porttitor libero sodales quam sagittis, id facilisis lectus semper.
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
File file = new File("C:\\TextFile.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
if (dis.available() != 0) {
// Get the line.
String s = dis.readLine();
// Put words to array.
String[] sParts = s.split(" ");
// Initialize word longest length.
int longestLength = 1;
for (String strx : sParts) { // Go through each sPart, the next one is called strx
// If the document has word longer than.
if (longestLength < strx.length())
// Set new value for longest length.
longestLength = strx.length();
}
// Because array index from "0".
int[] counts = new int[longestLength + 1];
for (String str : sParts) {
// Add one to the number of words that length has
counts[str.length()] += 1;
}
// We use this type of loop since we need the length.
for (int i = 1; i < counts.length; i++) {
System.out.println(i + " letter words: " + counts[i]);
}
}
}
}
// Result:
// 1 letter words: 0
// 2 letter words: 2
// 3 letter words: 0
// 4 letter words: 1
// 5 letter words: 0
// 6 letter words: 2
// 7 letter words: 2
// 8 letter words: 0
// 9 letter words: 3