setting a string's char' by index - java

I have an ArrayList of strings, and I want to randomly change a string's (random) char' by index.
ArrayListName.get(i).charAt(j)
exists. What's the equiv' for a setter?

Strings are immutable so they can't change. You can use a StringBuffer or Character Array. StringBuffer has a setCharAt method that takes an index and character.
If you want to use a String you'll have to create a new String with the character changed and replace the old String.

String is immutable. If you want to change the specific location's string, then you would want to use String.replaceAll() and capture the results in your ArrayList.

Related

Can a special character can be taken in a character variable with an alphabet?

I want to take input in a character variable which is A#.
Is it possible to do that?
Example:
char E[]={'E','F#','G#','A','B','C#','D#'};
To solve this I have taken array type as string. Which is giving me problem to get it's ascii to calculate hash value and also with respect to sorting as well.
No, you should use String to store these since there are more than 1 character. A char can only have 1 character. You can use the default hashCode() implementation of String for hash value and the default compareTo() for sorting.
You can write any character, include specials characters... For example 'Ñ'. But you can't write 'F#' in a char.
You can't store two characters in one char variable. Remember that 'F#' isn't a char, it's a String ! You should use String to store them. Then you can use compareTo() method for check if two strings are equal or not. Check This out

String replacement with "" Java

Say I have a string, String x = "oncetherewasaboy";
I want to replace "there" with "". I tried x.replace("there",""); But this does not remove it. This is not the best example but I am doing it for an array of strings and want to clean and filter some of the information inside each index with "". Thanks!
Strings are immutable. Calling replace() doesn't change the string. It creates a new one, and returns it. You need to save the reference to it in a variable, or else you can't access it. You need:
x = x.replace("there", "");
String is an immutable Type. An immutable object is an object whose state cannot be modified after it is created. So you can not modify the string x.
So as you can not modify the string, the replace method returns a new string with the new changes. So as jlordo has already suggested you need to use like following
x = x.replace("there", "");

How can I compare a string and a char array in Java?

In my program I'm trying to compare my char array asterixA[] to a String word in an if condition like:
if (word.equals(asterixA))
but it's giving me an error. Is there any other way I can compare them?
you have to convert the character array into String or String to char array and then do the comparision.
if (word.equals(new String(asterixA)))
or
if(Arrays.equals(word.toCharArray(), asterixA))
BTW. if is a conditional statement not a loop
You seem to be taking the "A String is an array of chars" line too literal. String's equals method states that
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
It all depends of the circumstances, but generally you compare two objects of the same type or two objects belonging to the same hierarchy (sharing a common superclass).
In this case a String is not a char[], but Java provides mechanisms to go from one to the other, either by doing a String -> char[] transformation with String#toCharArray() or a char[] -> String transformation by passing the char[] as a parameter to String's constructor.
This way you can compare both objects after either turning your String into a char[] or vice-versa.
You can compare the arrays:
if (Arrays.equals(asterixA, word.toCharArray()) {}
do as follows: if (word.equals(new String(asterixA))) { ... }

Converting char array into a string

I am making a hangman game. I have two char arrays and I need to check if they are equal.
One of them has letters and underscores: char checkLetter[]
The other one has only letters: char currentWord[]
Eventually, after the user has guessed all the words in the checkLetter[] array it will also consist of only letters. But I need to keep continually checking (in a boolean method) if the array into which they guess and their letters get stored, is the exact same as the word they are trying to guess.
If I could convert both of the arrays into strings then I could check them for equality. I am not experienced, and I don't know how to do this. Any help help would be appreciated!
You don't need to convert them to strings at all. Use Arrays.equals().
you can convert an char array into string using String's overloaded constructor which takes char[] array as argument.
char[] carr ;
String s = new String(carr);
You may use new String(char[] value)` to create String from char array.
Use the String-constructor:
String str = new String(yourCharArray);
However, that's useless; use Arrays.equals(arr1, arr2) instead.
You really don't need to convert the array, but if you really want to then
try using String word = currentWord.toString() to convert the char array.

Can I add a char to a variable specified position within a string?

OK, this is the line I am working on:
newstring.charAt(w) += p;
trying to add a character/char (p) to the string 'newstring' at a particular position within the string which is defined by int 'w'. Is this possible?
Strings are immutable in Java, so the answer is no. But there are many ways around it. The easiest is to create a StringBuilder and use the setCharAt() method. Or insert() if you want to insert a new character at a given position.
If you make multiple modifications to your string, you can (and indeed should) reuse your StringBuilder.
Well, you can't modify your string, because Strings are immutable in Java. If you try to change the string, you will get a new string object as a result.
Now, you can use String#substring method for that, using which you can get new string which is generated by some concatenation of substring of original string.: -
str = str.substring(0, w) + "p" + str.substring(w);
But, of course, using StringBuilder as specified in #biziclop's answer is the best approach you can follow.

Categories