Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8975295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:50:26+00:00 2026-06-15T18:50:26+00:00

I am practicing some exercises for my CS final, and am stuck on this

  • 0

I am practicing some exercises for my CS final, and am stuck on this problem, where I have to read a string, get a minimum length from the user, and return the amount of words that have at least that many letters. It seems as if my code is fine, but it can’t print out the answer. Can anyone help me out?

public class WordCount {



    public static void main (String [] args) {
        System.out.println("Enter a string: "); 
        String input =  IO.readString();


        System.out.println("Enter minimum word length");
        int wordlength = IO.readInt();
        int count = 0 ;
        do  {

            for (int i = 0 ; i < input.length(); i ++) {

                if (input.indexOf(i) == ' ') {

                    String check = input.substring(0, i);
                    if (check.length() >= wordlength) {

                        count++;
                        input = input.substring(i);
                        break;

                    }
                }

                    }

        } while (input.length() > 0);


    System.out.print("Words longer than " + wordlength + " characters: " + count);

    }

}

It seems as if the while loop runs infinitely, but I can’t figure out why!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:50:27+00:00Added an answer on June 15, 2026 at 6:50 pm

    Currently there are few problems with your code that I’ll point out first: –

    if (input.indexOf(i) == ' ')
    

    In the above statement, you should use String#charAt method, to get a character at a particular index. String#indexOf method is used for reverse process, i.e, you have a character and you want to find it’s index.

    Secondly, you are modifying your input inside the loop itself. And you are using the length of input in the terminating condition in the loop. You should not do anything like this.
    Rather, you can use another variable, which will store the index of last word you processed. And use that index in your substring method.

    Thirdly, you don’t really need a do while loop here. Your for loop itself is iterating over all your characters. Just remove the break from your if, which is really not required.

    So, your code will be modified to this: –

    int oldIndex = 0;  // to maintain the end index of previous word.
    int length = input.length();
    for (int i = 0 ; i < length; i ++) {
    
              if (input.charAt(i) == ' ' || i == length - 1) {
    
                    // If the word is at the end, then probably your first 
                    // condition in above `if` would fail, that is why I used a 
                    // second condition, which checks the end of string
    
                    // Now for the end of the string, we would need to use a single
                    // arguement substring method to get the word till the end.
                    // hence the below conditional expression.
    
                    String check = (i == length - 1)? input.substring(oldIndex): 
                                                    input.substring(oldIndex, i);
    
                    oldIndex = i + 1;  // Set oldIndex to the next index.
    
                    if (check.length() >= wordlength) {
    
                        count++;
                        //  input = input.substring(i);  Don't do this
                        // break;   // Don't break too.
    
                    }
               }
    
    }
    

    Now this was the modification of your code, so that you can learn what was your mistake.

    However, you have a pretty easy way to get what you want. You can use String#split method to split your string on space, which will return you an array of all the words, and you can operate on those words.

    It works something like this (in case you can use it): –

    String[] words = input.split(" ");  // split input string on space
    
    for (int i = 0; i < words.length; i++) {  // iterate over array
        if (words[i].length() >= wordLength) {
            count++;
        }
    }
    
    System.out.println(count);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using g++ on fedora linux 13. I'm just practicing some exercises from my
I have been practicing TDD and (some) XP for a few years now and
I am practicing making a web app which tries to read the user's twitter
I am practicing with PHP and AJAX but I have some problems! I'm trying
I've been doing some exercises from a book and I'm wondering if you could
I'm practicing some XSL and using this XML document as a simple example: <?xml
I am practicing some bool functions and I seem to be stuck any help
I have been practicing some old C++ problems to prepare for a few job
I'm practicing object oriented syntax in javascript and am having some problems. This is
I've been practicing some animation on Android but I could not get my code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.