user2880877
user2880877

Reputation:

Input sentence and print out number of words that are above min length requirement in java

My goal is to create a code that accepts a string, and the minimum number of characters allowed for each word. The output will be an int that tells the users the number of words in their sentence that was above or equal to the min they entered.

Now, my approach to this was to break the sentence up into individual word in the main method, then send each of those words into another method that will count the number of characters.

I am having difficulties in my main method, specifically splitting the sentence into individual words. I want to achieve this without using an array, only loops, substring and indexOf, etc. I commented the section of code that I am having issues with. I tested the rest of my code using a string with only one word, and my letterCounter method seems to be working fine. I know the answer is probably simple, but I am still having trouble figuring it out.

Any help would be wonderful! Thank you!

Here is my code:

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;
 }

}

Upvotes: 0

Views: 1492

Answers (2)

Matt Penna
Matt Penna

Reputation: 117

You could use string.split() like this to accomplish this:

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

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

    if(currentWord.length() >= min){
        finalcount++;
    }
}

Upvotes: 0

MrBackend
MrBackend

Reputation: 667

Have a a look at String.split()

Upvotes: 1

Related Questions