Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By jim829

Thread: Removing chars from a string

  1. #1
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Removing chars from a string

    I'm trying to write a program that will create a cipher using a key word. To do this, I need to change my standard alphabet (String plaintext) into the cipher alphabet. This is done by placing the key word (with repeated letters removed, which I have already done in removeDups) in front of the standard alphabet without any of the letters from the keyword.

    Basically, if my keyword is MARKER, my cipher alphabet will be MARKEBCDFGHIJLNOPQSTUVWXYZ.

    This is to be done in initCipherText. However, I can't figure out how to remove specific chars from a string. Is there a Java method for this, or will I need to do it with a loop?

    Java Code:
    import java.util.*; 
    import java.lang.String; 
    public class Cipher
    {
        String key = new String(); 
        String ciphertext = new String(); 
        final String plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
        Scanner in = new Scanner(System.in);
        
        
        public Cipher(String key) //Constructor
        {
            this.key = key;
            ciphertext = initCiphertext();
        }
        
        public String initCiphertext()
        {
            key = key.toUpperCase();
            key = removeDups(key);
            
            return null;
        }
        
        public static String removeDups(String s)
        {
            String result = "";
            for(int i = 0; i < s.length(); i++){
              String c = s.atChar(i) + "";
              if(!result.contains(c))
                result = result + c;
            }
            return result;
        }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Removing chars from a string

    Remember that Strings are immutable (cannot be changed). Therefore you will need to make a new String that only contains the chars you want. It maybe possible to do this using a regex and the replaceAll method or you can loop over each char one at a time and only add it to the new String if it is valid. Note: if you are going to build up a new String char by char it would be better to use StringBuilder.

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Removing chars from a string

    Rokr... Note that there's no atChar() method in the String class.... It is charAt(int index) method

    I'm afraid your code is still far from what you are trying to accomplish... include main method just to constantly test your code as you go on
    Last edited by Kagiso; 06-24-2013 at 01:24 PM.

  4. #4
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Removing chars from a string

    @Kagiso: Mental mistake- I meant charAt(). Good catch. Also, I have several other methods including a main written, but I just didn't include them in my post for brevity's sake.

  5. #5
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Removing chars from a string

    Quote Originally Posted by RoKr93 View Post

    Java Code:
        public String initCiphertext()
        {
            key = key.toUpperCase();
            key = removeDups(key);
            
            return null;
        }
    I think the problem is at the above method...
    Why do you want your method to return null?

    And one more thing... it may be irrelevant but I'm gonna say it anyway
    There's no need for you to import java.lang

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Removing chars from a string

    It's working what's wrong with this code ??? and one more thing why you've made
    Java Code:
    removeDups(String s)
    function static as you are using it in the same class I think ... and as mentioned by Kagiso you don't need to return null just make it void if the only purpose of this function is to remove dumps and you can use "Key" directly in you removeDups method without passing it as argument .
    Last edited by allaudin; 06-30-2013 at 04:36 PM.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Removing chars from a string

    Use a Set<Character> and concatenate the String "ABC ... Z" to your String. apply the following algorithm:

    1) for each character c in the String.toUpperCase()
    2) if c is not in your set, print it
    3) add c to your set

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Removing chars from a string

    JosAh I think he has mentioned that he has several other methods written which he doesn't post ... and also this function only removes duplicates, so there is no need to concatenate strings here ...

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Removing chars from a string

    Quote Originally Posted by allaudin View Post
    JosAh I think he has mentioned that he has several other methods written which he doesn't post ... and also this function only removes duplicates, so there is no need to concatenate strings here ...
    Yes, the concatenation is essential here; try to understand my algorithm.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Removing chars from a string

    I am talking about his algo ...

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Removing chars from a string

    Quote Originally Posted by allaudin View Post
    I am talking about his algo ...
    If so, then I don't understand your conclusion w.r.t. you previous reply as a respnse to my reply "so there is no need to concatenate strings here ..."; note that I didn't say "physical concatenation" per se.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Removing chars from a string

    You don't need to remove any characters, just ignore ones you already have. All you need is an instance of StringBuilder. You simply prepend the keyword to the alphabet and then iterate thru the combined string. If the index of the current character in the StringBuilder is >= 0, then ignore the character, otherwise append it to StringBuilder instance. I haven't checked this for speed or efficiency. This is similar to Jos' algorithm except it doesn't use a set.

    Regards,
    Jim
    Kagiso likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2011, 03:46 AM
  2. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  3. Make String into chars
    By myst in forum New To Java
    Replies: 19
    Last Post: 06-20-2010, 04:24 PM
  4. Swap chars in a String?
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 06-08-2010, 09:05 PM
  5. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •