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)
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
Code :
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
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
Re: String manipulation help
Thanks for the great help!
Ive tried this:
Code :
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
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
Code :
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.
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.
Code :
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]
}
}
}
}
Re: String manipulation help
Quote:
Originally Posted by
helloworld922
@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.
Code :
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.