I have the user input a sentence (example " Hello this is some jello "). I already have that complete by having a method to do this. What the method does is take a user input (.nextLine) then stores the words of the sentence into String[] words and I use the split function to break up the sentence into individual words. What I want to do is find in a given word, how many characters are in the word (length method without using .length()), then read in a sentence(already created a method to obtain user input then I called it in the main to execute), put each word into an object(Maybe a loop but where? I tried Word[] words = new Word[1]; Word is the public class and words comes from String[] words if that is correct, but I get LWord;32c8f6f8), then I can call the function that was created to get the length of the word in the object. Once I know the lengths I want to show the user the words that have the same length. Just looking for a push in the right direction to see if I can come up with anything I apologize if this is confusing and I will clarify it there are any questions. Thanks!

Recommended Answers

All 8 Replies

try by overriding Object's toString method. if you don't, the application 'll just print the reference to the Object, and that looks like what you described above.

The toString() method would be in the Word class.

Right now what I am doing the below

String[] words = sentence.split(" ");

//User Input: Hello this is some jello
// output:
// Hello
// this
// is
// some
// jello

So I have the sentence stored and split here. Now I want to find the amount of characters each word has (do I create a loop to go through the array and pull each word) so that I can compare them in a length method to show the user the words that are the same length.

if you want to know it for each element, you'll have to iterate over it indeed.

System.out.println(list.get(i));
// This get the list of words

Now how do I program it so that I can have the length method(that I already coded to find the length of one word) go through each of the arryList strings and record its length and then display the strings with the same length. I know how to do it hard coded with one word only :(

eg in the Main method

System.out.println(word);//word is "java"
    w1.length();//

I'm probably being confusing I will try my best to clarify if anyone needs it. Thank You!

Can you give an example of the input and desired output?
Are the Strings in an arrayList?
What order do you want the Strings displayed in?

The sentence is split into words and stored into an arrayList. An example input would be "Good day to you" output would be "found two words with similar length day and you" (if there are no matches then it should return -1).

Okay so I am able to call

Word word1 = new Word("hello"); // coded a single word in for testing purposes
word1.length()

All I need help with is being able to replace "hello" with the words from my arrayList. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.