I will type first the problem

"Let the user Enter a Sentence. then determine the longest word(s) in that sentence "

ex.#1 Input (Who is john galt ? )
output (john galt )
ex.#2
Input (Lets get jiggy with it)
Output (jiggy)

I already made a process for getting the longest words and outputting it..
The Only problem I can't solve for now.. is that I can't store The words in an array

This is what I have in mind

for(x=0;x<a.length();x++)
	{if(a.charAt(x)!=' ')
		{
		iba[y]+=a.charAt(x);
		}
	else if(a.charAt(x)==' ')
		{y++;}

but I always encounter an error
ArrayIndexOutOfBoundsException error

can you help me store the words in an array?

Recommended Answers

All 4 Replies

i'm trying to run your code in my head (i'm not at my pc), but i'm in weekend mode, so it's not working. :)

if you check the apis for String, you see there's a method that will convert a normal string to an array. also, you can use StringTokenizer to do the same thing.

i'm trying to run your code in my head (i'm not at my pc), but i'm in weekend mode, so it's not working. :)

if you check the apis for String, you see there's a method that will convert a normal string to an array. also, you can use StringTokenizer to do the same thing.

thanks for the info... but now... a new problem has arrived!

WOT IS stringtokenizer?

StringTokenizer allows you to take a string and break it up into "tokens".

eg:

if you have this string:

String str = "i+have+a+banana+in+my+wallet"

you tell the tokenizer that it must be broken up and the delimiter is "+".

StringTokenizer tok = new StringTokenizer( str, "+" );

then you just have to ask if it hasMoreTokens() and then getNextToken().

easy peasy.

easy peasy.

Yes, that is how you failed to answer main question of storing strings in array.

patricksquare, you can use Array in the case you know how many elements you will have. In your scenario this would involve additional count which is not necessary if you simply use ArrayList which can automatically grow as you add new elements, then if there is any specific reason for which you want to have your text in Array representation you can call method ArrayList.toArray()

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.