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 / February 2007

Tip: Looking for answers? Try searching our database.

delete repeated letters in a word

Thread view: 
spidey12345 - 08 Feb 2007 14:08 GMT
so If i enter the letter blbabh

the word will acutally become: blah

// repeatble letters in the key

   char checkkey[] = pkey.toCharArray();

   int pkeysize = checkkey.length;
    int counter = 0;
    for(int i = 1; i< pkeysize; i++)
    {
        for(int j = 0; j< i; j++)
        if(checkkey[i] ==checkkey[j])
        {
         checkkey[i] = '0';
        }
    }

// what i did here was just assign all repeated b's to 0 so i would
have
// bl0a0h

    for(int i = 0; i<pkeysize; i++)
    {
        System.out.println(checkkey[i]);
        if(checkkey[i] == '0')
            counter++;
    }

// it works up to this point, i get bl0a0h

// some thing is wrong with the aglorithm here....

    System.out.println(counter);
    int nsize = pkeysize-counter;
    char nkey[] = new char[pkeysize];
    for(int i = 0; i< pkeysize; i++)
    {
        int j = 0;

        if(checkkey[i] != '0')
        {
            nkey[j] = checkkey[i];
            j++;
        }
    }

    for(int i = 0; i< nsize; i++)
    System.out.print(nkey[i]);

// but when i try to print out the nkey, it gives me bunch of sh.t
Andrew Thompson - 08 Feb 2007 15:17 GMT
> so If i enter the letter blbabh
...
> // but when i try to print out the nkey, it gives me bunch of sh.t

Did you have a question, or were you just
'keeping us informed' of your progress?

Andrew T.
spidey12345 - 08 Feb 2007 17:35 GMT
> > so If i enter the letter blbabh
> ...
[quoted text clipped - 4 lines]
>
> Andrew T.

yeah i have a question, i am not sure what to do when i reach
"bl0a0h", read my comments the algorithm

so i am not sure why it's not reading correctly in that part of the
code

when i say if(checkkey[i] != '0') , and going through i iterations,
shouldn't it only set the ones that is not equal to 0 to the indexes,
why is it still
setting 0 to the answer

so when i print out the new key, it gives me bla0 rather than blah for
some reasons
Oliver Wong - 08 Feb 2007 17:48 GMT
> so If i enter the letter blbabh
>
> the word will acutally become: blah
[code snipped]

   First thing that comes to mind for me is to put the characters in a
LinkedHashSet, and then pull them out again in the correct order.

   - Oliver
spidey12345 - 08 Feb 2007 18:58 GMT
> > the word will acutally become: blah
>
[quoted text clipped - 4 lines]
>
>     - Oliver

Does java have any standard libary for this, i am new to java, so i
delt linkedhashset would be something i like to use.
Oliver Wong - 08 Feb 2007 19:06 GMT
>> enter the letter blbabh
>>
[quoted text clipped - 9 lines]
> Does java have any standard libary for this, i am new to java, so i
> delt linkedhashset would be something i like to use.

Yup: http://java.sun.com/javase/6/docs/api/java/util/LinkedHashSet.html

   - Oliver
spidey12345 - 09 Feb 2007 04:33 GMT
> >> "spidey12345" <swq_sha...@hotmail.com> wrote in message
>
[quoted text clipped - 18 lines]
>
> - Show quoted text -
ok, fine, i am using hashset:)

but which function in there allow me to delete repeated values or 0,
that is not character
Lew - 09 Feb 2007 07:04 GMT
> ok, fine, i am using hashset:)
>
> but which function in there allow me to delete repeated values or 0,
> that is not character

add()

Read the documentation for Set:
<http://java.sun.com/javase/6/docs/api/java/util/Set.html>

You will note the very first phrase of the explanation for Set: "A collection
that contains no duplicate elements."

The "0" was an artifact of your first approach and not part of the original
problem definition.

- Lew
Michael Rauscher - 09 Feb 2007 12:10 GMT
> ok, fine, i am using hashset:)
>
> but which function in there allow me to delete repeated values or 0,
> that is not character

Why do you use an array? You don't even need a HashSet if speed doesn't
matter.

Untested (I even didn't tried to compile it) code:

public String removeDuplicateLetters( String word ) {
    if ( word == null )
        return null;

    StringBuilder builder = new StringBuilder();
    for ( int i = 0, n = word.length(); i < n; i++ ) {
        String sub = word.substring(i,i+1);
        if ( builder.indexOf(sub) == -1 )
            builder.append(sub);
    }

    return builder.toString();
}

If you want to use a HashSet due to performance reasons the only thing
you'd have to replace is the if-condition and of course, you'd have to
add the element to the Set.

Bye
Michael
Daniel Pitts - 12 Feb 2007 21:22 GMT
> > ok, fine, i am using hashset:)
>
[quoted text clipped - 27 lines]
> Bye
> Michael

This might be a little better:
Its known to compile and run, for one thing.
public class Test2 {
   public static void main(String[] args) {
       final String s = "My string has a lot of duplicate letters";
       final StringBuilder builder = new StringBuilder(s);
       for (int i = 0; i < builder.length(); ++i) {
           final String charStr =
Character.toString(builder.charAt(i));
           int index = builder.indexOf(charStr, i+1);
           while (index > i) {
               builder.deleteCharAt(index);
               index = builder.indexOf(charStr, index);
           }
       }
       System.out.println(builder.toString());
   }
}
Bart Rider - 12 Feb 2007 17:15 GMT
> so If i enter the letter blbabh
>
[quoted text clipped - 36 lines]
>     {
>         int j = 0;

Move this statement out of the loop, than it will behave
like you want.

>         if(checkkey[i] != '0')
>         {
[quoted text clipped - 5 lines]
>     for(int i = 0; i< nsize; i++)
>     System.out.print(nkey[i]);

Hint: form a string out of the new char-array and print this.

> // but when i try to print out the nkey, it gives me bunch of sh.t

Think about it: what happens if your word contains the character
'0'? Maybe you should consider using '\0' instead of '0'.

Best regards,
Bart


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.