Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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中输入句子并打印出超过最小长度要求的字数_Java_String - Fatal编程技术网

在java中输入句子并打印出超过最小长度要求的字数

在java中输入句子并打印出超过最小长度要求的字数,java,string,Java,String,我的目标是创建一个接受字符串和每个单词允许的最小字符数的代码。输出将是一个int,它告诉用户句子中的字数高于或等于他们输入的最小值 现在,我的方法是在主方法中将句子分解成单个单词,然后将每个单词发送到另一个方法中,该方法将计算字符数 我在我的主要方法上有困难,特别是把句子分成几个单词。我希望在不使用数组、仅使用循环、子字符串和indexOf等的情况下实现这一点。我对我遇到问题的代码部分进行了注释。我用一个只有一个单词的字符串测试了我的其余代码,我的letterCounter方法似乎工作得很好。我

我的目标是创建一个接受字符串和每个单词允许的最小字符数的代码。输出将是一个int,它告诉用户句子中的字数高于或等于他们输入的最小值

现在,我的方法是在主方法中将句子分解成单个单词,然后将每个单词发送到另一个方法中,该方法将计算字符数

我在我的主要方法上有困难,特别是把句子分成几个单词。我希望在不使用数组、仅使用循环、子字符串和indexOf等的情况下实现这一点。我对我遇到问题的代码部分进行了注释。我用一个只有一个单词的字符串测试了我的其余代码,我的letterCounter方法似乎工作得很好。我知道答案可能很简单,但我还是很难找到答案

任何帮助都太好了!谢谢大家!

这是我的密码:

public class Counter
{
public static void main(String [] args)
{
    int finalcount = 0;

    System.out.print("Enter your string: ");
    String userSentence = IO.readString();


    System.out.print("Enter the minimum word length: ");
    int min = IO.readInt();

    //Error checking for a min less than 1

    while(min < 0)
    {
        IO.reportBadInput();
        System.out.print("Enter the minimum word length: ");
        min = IO.readInt();
    }


    int length = userSentence.length(); // this will get the length of the string



    for(int i = 0; i < length; i ++)
    {
         if (userSentence.charAt(i) == ' ')
         {  

             /* I dont know what to put here to split the words!

                  once I split the userSentence and store the split word into
                  a variable called word, i would continue with this code: */

            if((letterCounter(word)) >= min)
                finalcount++;

            else
                finalcount = finalcount;

           }
    }       




    IO.outputIntAnswer(finalcount);

}

//this method counts the number of letters in each word

    public static int letterCounter (String n)
        int length = n.length(); 
        int lettercount= 0;


     for(int i = 0; i < length; i ++)

    {
        boolean isLetter = Character.isLetter(n.charAt(i));

         if (isLetter) 
         {
            if ((n.length()) >= length) 
            {
                lettercount++;
            }
         }

    }

    return lettercount;
 }
公共类计数器
{
公共静态void main(字符串[]args)
{
int finalcount=0;
System.out.print(“输入字符串:”);
String UserSession=IO.readString();
System.out.print(“输入最小字长:”);
int min=IO.readInt();
//检查最小值是否小于1时出错
而(最小值<0)
{
IO.reportBadInput();
System.out.print(“输入最小字长:”);
min=IO.readInt();
}
int length=UserSession.length();//这将获得字符串的长度
for(int i=0;i=min)
finalcount++;
其他的
finalcount=finalcount;
}
}       
IO.outputIntAnswer(最终计数);
}
//此方法计算每个单词中的字母数
公共静态整数字母计数器(字符串n)
int length=n.length();
int-lettercount=0;
for(int i=0;i=length)
{
字母计数++;
}
}
}
返回信数;
}
}

看一看

您可以像这样使用string.split()来完成以下任务:

String [] splittedString  = inputString.split(" ");

for(int i = 0;i< splittedString.length; i++){
    String currentWord = splittedString[i];

    if(currentWord.length() >= min){
        finalcount++;
    }
}
String[]splittedString=inputString.split(“”);
for(int i=0;i=min){
finalcount++;
}
}

您的问题标题可能重复,这会产生误导。似乎你想计算一个句子中的单词数(并应用一些限制规则),但在描述中你说限制是与句子中每个单词的长度有关的。人们似乎不想费心去读这个问题,只想看标题,所以请修复它。虽然split返回一个数组,但它显然是不需要的。虽然我有我的疑问。哦,错过了那部分