Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

i want a code for this defition

Thread view: 
tan.like.friends@gmail.com - 08 Sep 2007 18:09 GMT
write a java program to remove a particular character from a string
that is given by user.
Knute Johnson - 08 Sep 2007 18:13 GMT
> write a java program to remove a particular character from a string
> that is given by user.

$20.  I take credit cards.

Signature

Knute Johnson
email s/nospam/knute/

Mark Space - 08 Sep 2007 19:24 GMT
>> write a java program to remove a particular character from a string
>> that is given by user.
>
> $20.  I take credit cards.

$20? You're cheap.  I'd charge $50. At least. Depends on how much
testing and how important efficiency is.  Always get the full spec.
Pulling characters out of really big string could be hairy if you want
something better than O(N^2).

And credit cards can easily be reversed.  I'd insist on a cashier's check.
Lew - 08 Sep 2007 19:36 GMT
tan.like.friends@gmail.com wrote:
>>> write a java program to remove a particular character from a string
>>> that is given by user.

Knute Johnson wrote:
>> $20.  I take credit cards.

> $20? You're cheap.  I'd charge $50. At least. Depends on how much
> testing and how important efficiency is.  Always get the full spec.
> Pulling characters out of really big string could be hairy if you want
> something better than O(N^2).
>
> And credit cards can easily be reversed.  I'd insist on a cashier's check.

Yeah, but then you could give the poster a low "star" rating.

To the OP:

Since it's a homework problem about String, the first thing to do is to check
out the API docs for String, and see if something in there could serve in your
algorithm.
<http://java.sun.com/javase/6/docs/api/java/lang/String.html>

Did you think about the algorithm?  What have you got for it so far?

Signature

Lew

Mark Space - 08 Sep 2007 23:56 GMT
> Yeah, but then you could give the poster a low "star" rating.

You're right.  I give the do-my-homework-for-me guy one star: *

And there it is.
nebulous99@gmail.com - 09 Sep 2007 04:50 GMT
> Pulling characters out of really big string could be hairy if you want
> something better than O(N^2).

Really? The only naive algo I can think of that's O(N^2) is more-or-
less Java-specific, and that's to keep reading a character, checking
if it's the one to omit, and if not:

String s;
...
   s = s + c;

which implicitly keeps copying the whole portion of the string built
so far for every added character.

The obvious, making s a StringBuffer or Builder and using .append(),
avoids this problem, though if you don't preallocate the buffer to the
size of the input string s it will get doubled in size involving a
copy now and again resulting in N log N performance. With
preallocation, it's strictly O(N). The naive C algorithm for doing the
same job mallocs a char array of the size of the input string and
advances pointers copying characters and is O(N).

(The final copy of a StringBuffer/Builder to a String in Java is also
O(N).)

So while the naive Java code for this is O(N^2) it's hardly "hairy" to
make it O(N); it doesn't require using non-java.lang API for heaven's
sake, and even the naive code for this task is typically O(N) in
languages whose "usual" string type is mutable.
nebulous99@gmail.com - 09 Sep 2007 05:07 GMT
On Sep 8, 11:50 pm, nebulou...@gmail.com wrote:
> The obvious, making s a StringBuffer or Builder and using .append(),
> avoids this problem, though if you don't preallocate the buffer to the
> size of the input string s it will get doubled in size involving a
> copy now and again resulting in N log N performance.

Bah -- most doubling-related algorithms cause a log N factor to appear
but this one looks like it does not. Adding up the amount copied in
toto gives you 2N - 1, *plus* log N for the overhead on each doubling,
if the allocation of a block of memory is O(1) as it should be in a
decent JVM. 2N + log N - 1 is O(N). It's still inefficient though,
about half as fast as with preallocation. Preallocation actually
wastes less memory on average too -- the worst case (every character
is omitted) wastes a full N bytes plus overhead, while the worst case
for reallocation has it double adding the final character that will be
added when no characters are omitted resulting in wasting at least N
bytes -- the next power of 2 higher than N minus one bytes I think.
The best case scenarios are the same, zero bytes wasted. Reallocation
also churns objects under the hood, while preallocation does not,
leaving more work for the GC at some point. So preallocation is still
best; use the underappreciated int-accepting constructor of
StringBuffer/Builder (both have this constructor).
Wildemar Wildenburger - 08 Sep 2007 18:15 GMT
> write a java program to remove a particular character from a string
> that is given by user.

The code? Easy

public class DYOH {
    public static void main(String[] argv) {
        System.out.printl("Do your own homework.");
    }
}

Also: <http://catb.org/~esr/faqs/smart-questions.html>

And finally: <http://java.sun.com/docs/books/tutorial/index.html>

Sorry for being so rude; just try a little harder next time, please.
Still, all of my suggestions here are serious.
/W
Joshua Cranmer - 08 Sep 2007 20:02 GMT
>         System.out.printl("Do your own homework.");
System.out.println("Do you own homework.");

Do I get a $2.56 check for finding a bug?
Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Christian - 08 Sep 2007 21:30 GMT
Joshua Cranmer schrieb:
>>         System.out.printl("Do your own homework.");
> System.out.println("Do you own homework.");
>
> Do I get a $2.56 check for finding a bug?
his name is Wildemar not Donald Knuth
Wildemar Wildenburger - 08 Sep 2007 22:31 GMT
>>         System.out.printl("Do your own homework.");
> System.out.println("Do you own homework.");

-> "Do you¡r! own homework."

> Do I get a $2.56 check for finding a bug?
Certainly. I'll send the check as soon as my $2.56 grammar correction
fee comes in.

/W
Joshua Cranmer - 09 Sep 2007 19:15 GMT
>>>         System.out.printl("Do your own homework.");
>> System.out.println("Do you own homework.");
[quoted text clipped - 6 lines]
>
> /W

I knew I should have copy/pasted...

BTW, I just sent you ZWD$2.56 yesterday...

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Ian Wilson - 10 Sep 2007 10:54 GMT
>>>>         System.out.printl("Do your own homework.");
>>>
[quoted text clipped - 12 lines]
>
> BTW, I just sent you ZWD$2.56 yesterday...

By the time he gets it, it will be worth ZWD$0.26 :-(
Martin Gregorie - 10 Sep 2007 17:29 GMT
>>>>>         System.out.printl("Do your own homework.");
>>>>
[quoted text clipped - 14 lines]
>
> By the time he gets it, it will be worth ZWD$0.26 :-(

It will still be worth ZWD$2.56 ....or about $0.001 in any other
dollar-using nation's currency.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Joshua Cranmer - 10 Sep 2007 22:17 GMT
> It will still be worth ZWD$2.56 ....or about $0.001 in any other
> dollar-using nation's currency.

The official exchange rate is ZWD$30,000 = $1 as of 9/6. Before that, I
believe it was about ZWD$250 = $1.

The black market rate is closer to about ZWD$600,000 = $1.

I remember reading once that a Zimbabwean was fretting because his
monthly salary was only ZWD$5M and the monthly rent was ZWD$2M. Well, at
least it's easy to be a millionaire in Zimbabwe...

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Lew - 11 Sep 2007 00:19 GMT
>> It will still be worth ZWD$2.56 ....or about $0.001 in any other
>> dollar-using nation's currency.
[quoted text clipped - 7 lines]
> monthly salary was only ZWD$5M and the monthly rent was ZWD$2M. Well, at
> least it's easy to be a millionaire in Zimbabwe...

Actually, you don't make it sound easy at all.  Easy to become one, surely,
but not to be one.

Signature

Lew

Jeff Higgins - 08 Sep 2007 22:37 GMT
tan.like.friends wrote:
> write a java program to remove a particular character from a string
> that is given by user.

OK

public class RCO
{
 public static void main(String[] args)
 throws Exception
 {
   if(args.length == 0)
   {
     System.out.println(
         "Usage: java RCO sed [OPTIONS] " +
       "{script-only-if-no-other-script} " +
       "[input-file]");
     System.out.println("Example: " +
       "java RCO sed -i 's/e//g' 'test'");
   }
   else
   {
     Runtime.getRuntime().exec(args);
   }
 }
}

or
<http://mindprod.com/jgloss/gettingstarted.html>
and
<http://java.sun.com/docs/books/tutorial/>
or
<http://www.rentacoder.com/>
Roedy Green - 09 Sep 2007 09:44 GMT
>write a java program to remove a particular character from a string
>that is given by user.

Do you mean.

1. remove the 10th char?

2. remove the first A?

3. remove all the A?

4. remove all the "*&#$"?

Have a peek the "strip" method in the DollarNumberFormatter part of
com.mindprod.spinner.

See http://mindprod.com/products1.html#SPINNER

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

kaldrenon - 09 Sep 2007 17:48 GMT
On Sep 8, 1:09 pm, tan.like.frie...@gmail.com wrote:
> write a java program to remove a particular character from a string
> that is given by user.

$string = s/$_//g;

YOU figure out how to make it a Java program.
Roedy Green - 11 Sep 2007 17:34 GMT
>write a java program to remove a particular character from a string
>that is given by user.

Perhaps for you English is a second language.  When you issue a bald
command like that, English-speaking people feel obligated to get angry
with you.

Ways of making the request polite include.

"I am looking for a Java program that..."

"Does anyone know how to write a program that.."

"Please, could someone write me a program that..."

English requires these circumlocutions.  When you leave them out, it
suggests you hold yourself in very high esteem and the person you are
asking the favour of to be your slave, with no choice in whether he
complies.

This is the way a two year old speaks when he is just learning the
language, and the limits of his power to control others.

As a toddler, I recall ordering my parents to "go to meeting" when I
was frustrated by their meddling interference. They just burst out
laughing.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 11 Sep 2007 18:33 GMT
> Ways of making the request polite include.
>
[quoted text clipped - 8 lines]
> asking the favour of to be your slave, with no choice in whether he
> complies.

In this English is a manifestation of a universal social phenomenon.  While
forms of (im)politeness vary between societies, they exist in all.

Roedy's site, http://mindprod.com/, has much more great advice in it.

To the OP: to make your communication even clearer, and draw the best
responses, include some details of what you've been attempting to date, and
for what specific situations you seek feedback.

Signature

Lew

Roedy Green - 12 Sep 2007 11:55 GMT
>In this English is a manifestation of a universal social phenomenon.  While
>forms of (im)politeness vary between societies, they exist in all.

The problem is when you violate the norm.  In Afghanistan for example
it takes about half an hour just to enter your office because of all
the greeting rituals you must go through with every other person
there.

In the Internet you get many clashes.  In India I was blown away with
the extreme respect people paid me, but were downright brutal with
women and the poor.

The anonymity of the newsgroup might tempt people to treat others as
lower status beings.  We appear to each other a bit like telephone
operators.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

nebulous99@gmail.com - 13 Sep 2007 06:54 GMT
On Sep 12, 6:55 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

> >In this English is a manifestation of a universal social phenomenon.  While
> >forms of (im)politeness vary between societies, they exist in all.
[quoted text clipped - 3 lines]
> the greeting rituals you must go through with every other person
> there.

No wonder they're still stuck in the late Stone Age, despite all the
recent aid and peacekeeping activities since the taliban fell.

Goes to show that the problem can be the norm itself sometimes,
especially norms derived from a time when those it applied to didn't
need to be themselves productive (feudal lords most likely).
Lasse Reichstein Nielsen - 11 Sep 2007 19:10 GMT
>>write a java program to remove a particular character from a string
>>that is given by user.
>
> Perhaps for you English is a second language.  When you issue a bald
> command like that, English-speaking people feel obligated to get angry
> with you.

It's somewhat excusable here, since it should be read as the continuation
of the subject, i.e., "... this definition: write a java program ...".

However, writing a message body that only makes correct sense when
read as following the subject is a *very* bad idea to begin with. Not
only because it's so easy to miss.

/L 'And punctuation and capitalization are great inventions too'
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

nebulous99@gmail.com - 13 Sep 2007 06:52 GMT
On Sep 11, 12:34 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> As a toddler, I recall ordering my parents to "go to meeting" when I
> was frustrated by their meddling interference. They just burst out
> laughing.

Hadn't learned the word "hell" yet, I take it? :)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.