Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: How to find all the words of a certain length in a String and return them.

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default How to find all the words of a certain length in a String and return them.

    Hello!

    What I need to do is given a input of a random text and a certain length,from the user I need to find all of the words in that text that have that length and return them. For example we input "I am having fun" and the user inputs 2, we should get "am" as a return.

    import java.util.Arrays;
    public class test {
    	public static String wordsOfLength(String words, int length) {
    		int cnt = 0;
    		String foundWords = "";
     
    		for(int i = 0; i < words.length(); i++) {
    			if(words.charAt(i) != ' ') {
    				cnt++;
    			}
    		}
    		for(int i = 0; i < words.length(); i++) {
    			if(cnt == length) {
    				foundWords += words.charAt(i);
    			}
    		}
    		return foundWords;
    	}
     
     
    	public static void main(String[] args) {
    		Out.print("Please input a text:");
    		String words = In.readString();
    		Out.print("Please input a length:"); 
    		int length = In.readInt();
    		Out.println(wordsOfLength(words,length));
    	}
    }
    This is my code an I am facing a problem where in the method that is suspossed to return the certain word,it just wont return the word.The return statements return the initalized foundWords at the very beggining and not the one after the for loop.I do not understand why this is so,some help would be great.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    How are you trying to debug the code to see what it is doing?
    Add some print statements that print out the values of variables as they are changed and used
    and also to see where the code's execution goes.

    The print out will help you see what the computer sees when the code is executed.

    Have you tried to design the program by making a detailed list of the steps the program needs to take to solve the problem? If so, please list those steps so we can see if the code is following the design.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    So my step by step solution

    1.Read in the text and the length
    2.Go through the text and find the length of the first word(than the second,third...)
    3.Check if the length of the first word is the same as the desired length. If yes;
    4.Add the charachters of the word into the a new string.
    5.Return the new string which contains the word.

    For debugging I've used the inteliJJ debugger but it didnt help,it simply tells me that all variables are initialized but not how or at what they contain at a certain point in time.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    .Go through the text and find the length of the first word
    Ok start with that one. How should the program do that?
    In the code: where is the length of the first word saved? Did you print it out to see if it was correct?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    import java.util.Arrays;
    public class test {
    	public static String wordsOfLength(String words, int length) {
    		int cnt = 0;
    		String foundWords = "";
    		for(int i = 0; i < words.length(); i++) {
    			if(words.charAt(i) != ' ') {
    				cnt++;
    			}
    			Out.println(cnt);
    		}
    		for(int i = 0; i < words.length(); i++) {
    			if(cnt == length) {
    				foundWords += words.charAt(i);
    			}
    		}
    		return foundWords;
    	}
     
     
    	public static void main(String[] args) {
    		Out.print("Please input a text:");
    		String words = In.readString();
    		Out.print("Please input a length:"); 
    		int length = In.readInt();
    		Out.println(wordsOfLength(words,length));
    	}
    }

    When I input "q" it prints out only 1 which is correct, when I input "this is a text" it prints out this

    1
    2
    3
    4
    4
    5
    6
    6
    7
    7
    8
    9
    10
    11

    it print out 2 times 4 and 6 because there is a ' ' and the count variable stays the same there.At those places is where the words are seperated.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    it prints out this
    What is the program counting? What is 11 a count of?

    What should the program do when it finds a space?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    11 is the count of the chars in the text.

    Well I'm aware that the program,when it entcounters a white space it should register that we have found a word.Not really sure how to do that.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    register that we have found a word
    Ok, what does the program need to do with the word it finds? Look at the reason the program is being written:
    find all of the words in that text that have that length
    When the program finds a word, what does it need to do?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    import java.util.Arrays;
    public class test {
    	public static String[] splitString (String s) {
    		return s.split (" ");
    	}
     
    	public static void wordsOfLength(String words,int length) {
    		String[] split = splitString((words));
    		for(int i=0; i <split.length; i++){
                if(split[i].length()== length){
                    System.out.print(split[i]+", ");
                }
             }
        }
     
        public static void main(String[] args) {
     
            System.out.print("Please input the text: ");  
            String words = In.readString();
    		System.out.print("Please input the length:");
    		int length = In.readInt();
            wordsOfLength(words,length);
        }
    }

    So I've got the code working,only thing is am printing the words out in a print statement and not returning them as value.Not sure how to incoperate that in this.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    returning them as value
    How are the word supposed to be returned?
    All in one String separated by spaces
    In an array(or ArrayList) of Strings
    One String at a time requiring that the method be called one time for each String to be returned
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    I can return them any way I would like,I'd prefer the easiest solution.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    the easiest solution.
    Build a String with the words separated by spaces: savedWords += newWord + " "
    Return that String to the caller.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    But what are my savedWords and what is my newWord? How do I "access" them ?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    what are my savedWords and what is my newWord
    savedWords is a new String that will be filled with the found words and returned by the method that finds the words.
    The newWord is what is found in the search for the word that has the desired length. Those words are in the split array.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    Getting this error test.java:14: error: cannot find symbol
    return savedWords;
    ^
    symbol: variable savedWords
    location: class test
    1 error

     
    import java.util.Arrays;
    public class test {
    	public static String[] splitString (String s) {
    		return s.split (" ");
    	}
     
    	public static String wordsOfLength(String words,int length) {
    		String[] split = splitString((words));
    		for(int i=0; i <split.length; i++){
                if(split[i].length()== length){
                    String savedWords = split[i] + " ";
                }
             }
    		 return savedWords;
        }
     
        public static void main(String[] args) {
     
            System.out.print("Please input the text: ");  
            String words = In.readString();
    		System.out.print("Please input the length:");
    		int length = In.readInt();
            Out.println(wordsOfLength(words,length));
        }
    }

    Quick edit:

    I've declared the variable on class level and had to make it static.Definetly not the most elegant solution but it works.

     
    import java.util.Arrays;
    public class test {
    	static String foundWords = "";
     
    	public static String[] splitString (String s) {
    		return s.split (" ");
    	}
     
    	public static String wordsOfLength(String words,int length) {
    		String[] split = splitString((words));
    		for(int i=0; i <split.length; i++){
                if(split[i].length()== length){
                     foundWords += split[i] + " ";
                }
             }
    		 return foundWords;
        }
     
        public static void main(String[] args) {
     
            System.out.print("Please input the text: ");  
            String words = In.readString();
    		System.out.print("Please input the length:");
    		int length = In.readInt();
            Out.println(wordsOfLength(words,length));
        }
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    The variable should be declared in the wordsOfLength method, just like the split variable. That is where it is filled and returned.
    Using global static variables is a bad solution.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    Oh okay,I didnt know I could do that.I'd assume this is the way

     
    import java.util.Arrays;
    public class test {
     
    	public static String[] splitString (String s) {
    		return s.split (" ");
    	}
     
    	public static String wordsOfLength(String words,int length) {
    		String foundWords = "";
    		String[] split = splitString((words));
    		for(int i=0; i <split.length; i++){
                if(split[i].length()== length){
                     foundWords += split[i] + ",";
                }
             }
    		 return foundWords;
        }
     
        public static void main(String[] args) {
     
            System.out.print("Please input the text: ");  
            String words = In.readString();
    		System.out.print("Please input the length:");
    		int length = In.readInt();
            Out.println(wordsOfLength(words,length));
        }
    }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    What happened with that code?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    It worked just fine.Is it not working on your end?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    I am glad you have it working now.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    Thank you,and also thank you for your help!

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find all the words of a certain length in a String and return them.

    You are welcome.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] string length problem
    By ma5sacre in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2014, 01:02 PM
  2. Replies: 3
    Last Post: June 26th, 2013, 02:12 AM
  3. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  4. Replies: 2
    Last Post: September 29th, 2012, 07:42 PM
  5. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM