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