• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary String

 
Travis Gn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have four binary string stored in an array:
[1100001]
[1100011]
[1100010]
[1100011]
I want to randomly change one bit of the String in each array. From 0 to 1 or 1 to 0. Is there an easy way to change one bit in a string? Thank you for any help.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that I am reading your question correctly, you are stating that you have an array containg four strings:

To answer your question: You can't. Strings are immutable objects; that means, once assigned a value, they cannot change.
Do not fear, however! Java is not a language that would leave you hanging like that. Use the StringBuffer class -- it is mutable.

(If you can follow how that bit of code works, then you're doing well....)
 
Travis Gn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help that worked perfectly. Do you know if you can create an array of stringbuffers?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know if you can create an array of stringbuffers?
Much like creating an array of Strings, or of anything else.
StringBuffer array = new StringBuffer[someSize];
You might want to read up on arrays in Java in the Arrays lesson of Sun's Java Tutorial. Another good introductory tutorial to reference is Bradley Kjell's Introduction to Computer Science using Java. Arrays start in chapter 46.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic