Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Strings help!! Posted by hello1212 on 31 Dec 2008 at 12:48 AM
I am doing a gui program.
How do i sort the words which have only the letter'e' in them??
Suppose i have the string,"Hello is a pleasing word"
I want only the words
1)Hello
2)Pleasing
HOW DO I DO THIS WITHOUT USING ARRAYS??????.
Ok and i have to write this in a method.

Here is the method's headerand it 's description
//Returns a string that concatenates all "offending"
// words from text that contain letter; the words are
// seperated by '\n' characters; the returned string
// does not contain duplicate words: each word occurs
// only once; there are no punctuation or whitespace
// characters in the returned string

public String allWordsWith(char letter)
Report
Re: Strings help!! Posted by vibin_mdcp on 31 Dec 2008 at 9:20 AM
: I am doing a gui program.
: How do i sort the words which have only the letter'e' in them??
: Suppose i have the string,"Hello is a pleasing word"
: I want only the words
: 1)Hello
: 2)Pleasing
: HOW DO I DO THIS WITHOUT USING ARRAYS??????.
: Ok and i have to write this in a method.
:
: Here is the method's headerand it 's description
: //Returns a string that concatenates all "offending"
: // words from text that contain letter; the words are
: // seperated by '\n' characters; the returned string
: // does not contain duplicate words: each word occurs
: // only once; there are no punctuation or whitespace
: // characters in the returned string
:
: public String allWordsWith(char letter)
:

Hai, Here is your allWordsWith function as you dreamed............!

public static String allWordsWith(String Text, char letter) {
String str = "", tmp, test = String.valueOf(letter);
int i = 0, j = -1;
do {
i = Text.indexOf(' ', j + 1);
if (i == -1) {
if (j < Text.length()) {
tmp = Text.substring(j + 1, Text.length());
if (tmp.contains(test) && str.contains(tmp) != true) {
str = str + tmp + '\n';
}
}
break;
}
tmp = Text.substring(j + 1, i);
if (tmp.contains(test) && str.contains(tmp) != true) {
str = str + tmp + '\n';
}
j = i;
} while (true);
return str;
}

This function takes two arguments, one a String Text and other a char letter. Returns only words containing the given letter and without any repetition.

Good luck.
But don't forget to thank me..........................................!
Report
Re: Strings help!! Posted by hello1212 on 31 Dec 2008 at 1:03 PM
THANNNNNNKKKKKKKKKKKKK YYYYYYYYYYYYYYOOOOOOOOOOOOOOOUUUU but can u explain the logic in the loop please????
Oh and by the way all works fine but for the puncuation part!!!
If i have a string,"Hello, is a good and, a pleasing, word.
The output will be:
1)Hello,
2)pleasing,

So how do i remove the commas and other puncuation marks???
Report
Re: Strings help!! Posted by virusadm on 31 Dec 2008 at 10:01 PM
Welll thanks for all that information and best of luck............?


: : I am doing a gui program.
: : How do i sort the words which have only the letter'e' in them??
: : Suppose i have the string,"Hello is a pleasing word"
: : I want only the words
: : 1)Hello
: : 2)Pleasing
: : HOW DO I DO THIS WITHOUT USING ARRAYS??????.
: : Ok and i have to write this in a method.
: :
: : Here is the method's headerand it 's description
: : //Returns a string that concatenates all "offending"
: : // words from text that contain letter; the words are
: : // seperated by '\n' characters; the returned string
: : // does not contain duplicate words: each word occurs
: : // only once; there are no punctuation or whitespace
: : // characters in the returned string
: :
: : public String allWordsWith(char letter)
: :
:
: Hai, Here is your allWordsWith function as you dreamed............!
:
: public static String allWordsWith(String Text, char letter) {
: String str = "", tmp, test = String.valueOf(letter);
: int i = 0, j = -1;
: do {
: i = Text.indexOf(' ', j + 1);
: if (i == -1) {
: if (j < Text.length()) {
: tmp = Text.substring(j + 1, Text.length());
: if (tmp.contains(test) && str.contains(tmp) !=
: true) {
: str = str + tmp + '\n';
: }
: }
: break;
: }
: tmp = Text.substring(j + 1, i);
: if (tmp.contains(test) && str.contains(tmp) != true) {
: str = str + tmp + '\n';
: }
: j = i;
: } while (true);
: return str;
: }
:
: This function takes two arguments, one a String Text and other a
: char letter. Returns only words containing the given letter and
: without any repetition.
:
: Good luck.
: But don't forget to thank
: me..........................................!
:

Report
Re: Strings help!! Posted by hello1212 on 31 Dec 2008 at 10:04 PM
yeah but what about the puncuation marks??
Report
Re: Strings help!! Posted by vibin_mdcp on 1 Jan 2009 at 9:45 AM
: yeah but what about the puncuation marks??
:

Uhmmmmmmmmmmmmmm...........

You people expecting too much from me.

Here is the logic.

Our allWordsWith(String Text,char letter) function takes two arguments of String and char. Here 'String Text' is the source and 'char letter' is to be searched for.

First this function converts given string into words(i.e parts of string seperated by a 'space'). Each word is stored into a String variable 'str' seperated by a new line charecter '\n'. Before storing, it checks whether the word contains the given letter and whether our 'word' is not added previously. This checking is done through the statement

if (tmp.contains(test) && str.contains(tmp) != true)

This procedure is repeted till the end of the String Text.

Now about your new problems. You have to check whether the word ends with a punctuation mark. For this add another if statements in the above if clause.

I hope you guis got the IIIIdea..... and can do remaining.........

hello1212 I got your Thank You.

Wish you good programing daysssssssssssss.......................

Don't forget the thanks.
Report
Re: Strings help!! Posted by hello1212 on 1 Jan 2009 at 2:48 PM
Yeah,but what will be the condition???
is this correct??

if( str.contains(",")||str.contains(".")||str.contains("!") != true)
Report
Re: Strings help!! Posted by vibin_mdcp on 2 Jan 2009 at 8:05 AM
: Yeah,but what will be the condition???
: is this correct??
:
: if( str.contains(",")||str.contains(".")||str.contains("!") != true)
:

Not str we copied a word to tmp. So you have to check whether tmp contains any punctuation marks. So it should be as follows

if( tmp.contains(",")||tmp.contains(".")||tmp.contains("!") == true)

If the condition is true then you have to remove the punctuation Okkkkkk...
So we have to make a change in tmp. So add following code before adding tmp into str

if( tmp.contains(",")||tmp.contains(".")||tmp.contains("!") == true){
tmp=tmp.substring(0,tmp.length-1);
}

Don't forget to add the above code in both if statements.
__________________________________________________________________________
Okkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk?
--------------------------------------------------------------------------


With Regards,
Vibin Varghese



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.