Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: String manipulation help

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String manipulation help

    Hi,

    I have a problem with some code im working on, which I'll explain now:

    I have an ArrayList<String> which holds multiple strings.

    Each string consists of 64 characters, randomly being a "0" or a "1".

    For example:

    11010111111001001001001100010011111000100001000001 00110100010010

    I want to be able to loop through my ArrayList and go to each character in every string and then calculate the following:

    With a probability of 1 in a 100 chance - if its a "1" change to a "0" and vice-versa.

    This is the way I tried to do it:

    StringBuilder sb = new StringBuilder();
     
            for (int i = 0; i < populationArray.size(); i++)
            {
                for (int j = 0; j < individualSize; j++)
                {
                    if (new Random().nextInt(100) == 1)
                    {
                        //TODO
     
                   }
                }
            }

    populationArray is the ArrayList
    and individualSize is equal to 64

    Thanks.


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: String manipulation help

    OK so how did you actually try to change it? I mean I think that logic is right but have you actually tried changing the characters yet? I would say you should declare a temporary string and reset it each time you loop through(the outer loop) and then add to it each loop through using populationArray[i].charAt(j). Then if the random thing kicks in use the same method to check if it is 0 or 1 and just add the other value to the string. Did any of that make sense? I'm pretty tired here so sorry if I didn't really explain very well.


    Take a look at this as well

    Manipulating Characters in a String (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String manipulation help

    Ive tried this. is this what you meant?
    Im getting an error at
    if (populationArray.get .charAt(j) = "0")

    ERROR: unexpected type. required variable. found value

    StringBuilder sb = new StringBuilder();
     
            for (int i = 0; i < populationArray.size(); i++)
            {
                for (int j = 0; j < individualSize; j++)
                {
                    //sb.append(populationArray.get .charAt(j));
     
                    if (populationArray.get .charAt(j) = "0")
                    {
                        if (new Random().nextInt(100) == 1)
     
    {
                            sb.append(populationArray.set(i, "1"));
     
                            System.out.println(sb);
                        }
                    }
                }
                //sb.delete(0, sb.length());
            }

    Thanks

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: String manipulation help

    Double quotes denote a String. Single quote denote a character. Also, you're missing some bits of code, such as calling the get method. Your logic could also use some fixing.

    here's the pseudo-code:

    1. for all strings in populationArray.
    2. for all characters in that string
    3. generate a random number [0-100) (how your code for generating a random number is right now). See if it equals 1.
    4. If it is:
    5. See if charAt(j) == '0'. If so, put a '1'. Else, put a '0'.
    6. end inner loop
    7. end outer loop

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String manipulation help

    Thanks for the great help!

    Ive tried this:

    for (int i = 0; i < populationArray.size(); i++)
            {
                for (int j = 0; j < individualSize; j++)
                {
                    if (new Random().nextInt(100) == 1)
                    {
                        if (populationArray.get .charAt(j) == '0')
                        {
                            populationArray.set(i, "1");
                        }
                        else
                       {
                            populationArray.set(i, "0");
                        }
                    }
                }
            }

    but isnt this going to work?
    should when I use set, do it differently?
    Thanks

  6. #6
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: String manipulation help

    The problem there is you are replacing the whole string with 1 character I believe.

    I don't think theres any way to just replace 1 character in a string. What you want to do is declare a temporary string which at the start of each outer loop is set to "" and then if the random generator doesn't kick in add the currect character, other wise add the other.

    So
    psuedo

    If Random = 1 then if character = 0 set append 1 to string if it is 1 append 0.
    Else if random != 1 then find character and append to string

    to append just use
    tempString += populationArray.get(i).charAt(j)

    And don't forget to put the tempString into the array afterwards.

    Also I think you weren't telling the array which string you wanted originally.

    However I may be misunderstanding the String.replace() function so check that out as well.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: String manipulation help

    @duff
    your code is almost right. Instead of setting the String at i to just a single 1 or 0, you have to get the i'th string and then set a specific character to 1 or 0. Here's a version that will work, I bolded the parts that were changed.

    for (int i = 0; i < populationArray.size(); i++)
            {
                for (int j = 0; j < [b]populationArray.get(i).length()[/b]; j++)
                {
                    if (new Random().nextInt(100) == 1)
                    {
                        if (populationArray.get[b](i)[/b].charAt(j) == '0')
                        {
                           [b] populationArray.get(i).setCharAt(j, '1');[/b]
                        }
                        else
                       {
                            [b]populationArray.get(i).setCharAt(j, '0');[/b]
                        }
                    }
                }
            }

  8. #8
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: String manipulation help

    Quote Originally Posted by helloworld922 View Post
    @duff
    your code is almost right. Instead of setting the String at i to just a single 1 or 0, you have to get the i'th string and then set a specific character to 1 or 0. Here's a version that will work, I bolded the parts that were changed.

    for (int i = 0; i < populationArray.size(); i++)
            {
                for (int j = 0; j < [b]populationArray.get(i).length()[/b]; j++)
                {
                    if (new Random().nextInt(100) == 1)
                    {
                        if (populationArray.get[b](i)[/b].charAt(j) == '0')
                        {
                           [b] populationArray.get(i).setCharAt(j, '1');[/b]
                        }
                        else
                       {
                            [b]populationArray.get(i).setCharAt(j, '0');[/b]
                        }
                    }
                }
            }
    Ah my bad I didn't notice(or know about) the StringBuilder seems like a handy little class alright. I would go with helloworld on this one seems simple and elegant and probably along the lines you were thinking.

Similar Threads

  1. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 10:16 AM
  2. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM