I do not know why I am getting an error at the yy.charAt(i) assignments. It says... Variable Expected... Not value.
static int subtract(int x,int y)
{
String yy=Integer.toBinaryString(y);
System.out.println(yy);
for(int i=0;i<yy.length();i++)
{
if(yy.charAt(i)==1)
{
yy.charAt(i)=0;
}
else
{
yy.charAt(i)
}
}
int t=Integer.parseInt(yy);
return(t);
}
You can't assign values to a string's index position, strings are immutable in Java. This will never work:
yy.charAt(i)=0;
If you need to modify a string, transform it to a char[] (using the toCharArray() method), modify the array and then build a new string from that array, using the String(char[]) constructor.
Alternatively, you could use a StringBuilder to modify the characters before returning a new string.
Use a StringBuilder instead.
The code would be almost identical to what you have now, except for these changes:
StringBuilder yy = new StringBuilder(Integer.toBinaryString(y));
...
yy.setChatAt(i, '0');
I think there are a few things that are not clear to you.
I think you mean the character '0' not the value 0.
The lines else { yy.charAt(i); } have absolutely no effect. You can simply omit them.
Strings are immutable in Java (i.e. they cannot be modified in place).
Even if they were, you're syntax is wrong. Something of the form class_name.method_name() is a call to a method of a class. It returns a value that you can store, it is not the same as a variable and trying to assign to a method call makes no sense at all.
To modify Strings in Java, the best way is probably to use a StringBuilder. You create a new StringBuilder using your String, make the necessary changes on that and then convert it back into a String.
So this would look something like this:
StringBuilder builder = new StringBuilder(yy); // StringBuilder from yy.
// rest of your code here
builder.setCharAt(i, '0');
// more code
yy = StringBuilder.toString(); // convert it back to a String.
Notice that even in a StringBuilder you have to call the appropriate method and pass in the value that you want to assign to it.
Related
I'm a beginner in Java I have some questions regarding the enhanced for loop and string arrays.
In the source code below, I've been trying to change the contents of the arrays using the enhanced-for loops.
It seems that I can't change the contents in the the String array(arrString), but I don't know exactly why. (It works fine for StringBuilder objects.)
I am a bit confused because I could actually do operations like str1+=str2 and change the String contents (although this operation is done via StringBuilder class) in normal situations.
Can anyone point out why this is happening and if there's any misunderstanding on my part?
class EnhancedForTest{
public static void main(String[] args){
//StringBuilder
StringBuilder[] arrStringBuilder=new StringBuilder[]{new StringBuilder("Hello1"), new
StringBuilder("Hello2"),new StringBuilder("Hello3")};
for(StringBuilder e: arrStringBuilder){
e.append("!");
}
for(StringBuilder e:arrStringBuilder){
System.out.println(e);
}
//String
String[] arrString=new String[]{"Hello1","Hello2","Hello3"};
for(String s:arrString){
s+="!";
}
for(String s:arrString){
System.out.println(s);
}
}
}
The results are as follows(Sorry I didn't add the results!):
Hello1!
Hello2!
Hello3!
Hello1
Hello2
Hello3
P.S.: I've taken out the printing line as a new for loop, it still works for StringBuilder but not String
On each iteration of the for-each loop the variable obtains a reference to the corresponding element of the array. In the case of for(StringBuilder e:arrStringBuilder) on the first iteration a new variable e of type StringBuilder will point to the same object as arrStringBuilder[0]. Then you call append using this reference, which performs operations on the object pointed by both references e and arrStringBuilder[0]. On the next iteration e will be assigned reference to arrStringBuilder[1], but the changes made in arrStringBuilder[0] will stay.
In the case of for(String s:arrString) on the first iteration a new variable s will be created pointing to arrString[0]. However, when you do s += '!' you actually perform s = s + '!', so you assign to this temporary variable s a reference to the new String object, which will contain Hello1!. This new object, however, is not related in any way to the original arrString[0] object, and at the start of the next iteration is just discarded. Your loop works like this:
for(int i = 0; i < 3; i++) {
// Create a temporary variable which points to the same object as arrString[i]
String s = arrString[i];
// Create a temporary object which keeps the result of concatenation of s and '!'
String temporary = s + '!';
// Replace the reference stored in s with the reference to the temporary object
s = temporary;
// Now there is no connection between s and arrString[i]
// And here we just discard both temporary objects 's' and 'temporary'
// arrString[i] object remains unchanged.
}
e.append("!"); is a call to a method, whose specific purpose is to modify e.
s+="!"; is not a method call. It is exactly equivalent to this:
s = s + "!";
This does not alter the state of any object. It merely creates a new String object (that is, a String whose text value is the old value of s plus "!"), and assigns that to the variable s. You are changing what s holds, but you didn’t modify the original String object that s used to hold.
It turns out there are no methods of String which will modify the String object, because Strings are immutable, by design. On the other hand, the StringBuilder class exists specifically to create and work with changeable text values.
public class ReverseString {
public static void main(String[] args) {
String s = "mnop";
s.charAt(0) = 'l';
}
}
Java only allows you to assign to variables, fields and array elements.
The result of a method - like s.charAt(0) - is none of these, so you can't assign to it.
The reason for this is down to the way Java returns: it returns by value, not by reference, and that value only exists temporarily. As such, if you were able to assign it, the side effect of that assignment is immediately lost, making it pointless.
It's also true that String is immutable; but this limitation on what you can assign to is the reason you couldn't do this even for some notional MutableString class you might try to create.
Strings in java are immutable, meaning they can't change at all.
To do something like this, use a StringBuilder:
StringBuilder sb = new StringBuilder("mnop");
sb.setCharAt(0, 'l');
//later, you probably want to get back to a String:
String s = sb.toString();
s.charAt(0) returns a char value, not a char variable to which you could assign a value.
And anyway, String is immutable, so you can't modify the value of an existing String.
You can obtain a copy of the array of all the characters of the String, and modify that array:
String s = "mnop";
char[] chars = s.toCharArray();
chars[0]= 'l';
However, this doesn't modify the original String, since it's immutable.
You can create a new String using that array though:
String newS = new String(chars);
charAt returns a char that's a copy of the character at that position in the string. It's not a reference back to the original string, which is immutable.
You could use a StringBuilder instead, though:
StringBuilder sb = new StringBuilder("mnop");
sb.setCharAt(0, 'l');
String s = sb.toString();
Reversing a string can be done by concatenating the Original String through a reverse loop (from str.length-1->0)
but why is this not Working Correctly :
by adding the character by character from last positon to the 0th position:
int i = 0;
while(i<originalStr.length())
{
strRev.charAt(i)=originalStr.charAt(str.length()-1-i);
i++;
}
Strings are immutable in Java. You cannot edit them.
If you want to reverse a String for training purpose, you can create a char[], manipulate it then instantiate a String from the char[].
If you want to reverse a String for professional purpose, you can do it like this :
String reverse = new StringBuilder(originalStr).reverse().toString();
strRev.charAt(i) // use to Retrieve what value at Index. Not to Set the Character to the Index.
All we know that String is a immutable class in Java. Each time if you try to modify any String Object it will Create a new one.
eg :- String abc = "Vikrant"; //Create a String Object with "Vikrant"
abc += "Kashyap"; //Create again a new String Object with "VikrantKashyap"
// and refer to abc again to the new Object.
//"Vikrant" Will Removed by gc after executing this statement.
Better to Use StringBuffer or StringBuilder to perform reverse Operation. The only Difference between these two class is
A) StringBuffer is a Thread Safe (Synchronized). A little slow because each time need to check Thread Lock.
B) StringBuider is not Thread Safe. So, It gives you much faster Result Because it is Not Synchronized.
There are Several Third Party Jars which provides you a Features like Reverse and Many more String base Manipulation Methods
import org.apache.commons.lang.StringUtils; //Import Statement
String reversed = StringUtils.reverse(words);
In your test method, best practice is to use triple A pattern:
Arrange all necessary preconditions and inputs.
Act on the object or method under test.
Assert that the expected results have occurred.
#Test
public void test() {
String input = "abc";
String result = Util.reverse(input);
assertEquals("cba", result);
}
I am working on a section of code for an assignment I am doing atm, and I am completely stuck with 1 little bit.
I need to convert the contents of an array list into a string, or the form of a string, which will be able to be imput into toString() in order for it to be printed to the screen.
public String toString(){
String full;
full = (this.name + this.address + "\n" + "Student Number = " + this.studentId);
for (int i = 0; i < cs.size(); i++) {
full.append(cs[i]);
return full;
The piece of above code is where i attempt to combine 3 varaibles and the contents of an array list into a single string with formatting.
Unfortunatly it creates an error "The type of the expression must be an array type but it resolved to ArrayList"
Thanks for any help.
Jake
cs is array list, so you have to do get operation, not [] (which is for array access)
It should be like:
full.append(cs.get(i));
Not
full.append(cs[i]);
EDIT: As assylis said, full should be StringBuilder not just String, because String doesn't support append() method.
StringBuilder full = new StringBuilder();
Apache Commons StringUtils has different varieties of join() methods that mean you don't have to write this yourself. You can specify the separator and even the prefix/suffix.
I would recommend you look at Apache Commons, not just for this but for lots of other useful stuff.
You are attempting to access an ArrayList as though it is a primitive array (using the square brackets around the index). Try using the get(int index) method instead.
i.e.,
cs.get(i);
You cannot index an ArrayList like an array, you need the get(index) method. Even better, use the enhanced for loop, since it's not recommended to index over a list, as the implementation may change to LinkedList.
I also suggest using a StringBuilder for efficiency:
public String toString() {
StringBuilder full = new StringBuilder();
full.append(this.name);
full.append(this.address);
full.append("\n");
full.append("Student Number = ");
full.append(this.studentId);
for (String s: cs)
full.append(s);
return full.toString();
}
Just use
"cs.get(i)" in place of "cs[i]".
as cs is an ArrayList not an Array.
and also use
full = full + cs.get(i); and not full.append(cs.get(i));
as String type dont have a append method.
Just a note, since you don't put any spacers between each element of the ArrayList it might be unreadable. Consider using Guava's Joiner class.
So instead of
for (...)
s.append(y);
if would be
a.append(Joiner.on(" ").join(yourList));
The Joiner is also more efficient than the for loop since it uses a StringBuilder internally.
I want to get the first 4 characters of a string to compare with another string. However, when I do something like
String shortString;
shortString = longString.subString(0,3);
It takes along longString's backing array and makes it impossible to compare easily.
I've also tried converting longString into a character array and inserting each character but I always seem to end up with long string. The Android Development documents say to use the String constructor to remove the backing array but it doesn't seem to work for me either.
String shortString = new String(longString.subString(0,3));
Any suggestions would be appreciated!
First, it's string.substring() not .subString().
Second, what do you mean "impossible to compare easily"? You can compare strings with .equals() easily.
public static void main(String[] args) {
String longString = "abcdefghijklmn";
String shortString = longString.substring(0, 3);
System.out.println(shortString.equals(longString));
}
this code prints false, as it should.
Update:
If you call .substring() so that it produces string of the same length as original string (e.g. "abc".substring(0,2)) than it will return reference to the same string. So, .equals() in this case will return true.
How would you want to compare? There's built in method for simple comparison:
longString.subString(0, 3).compareTo(anotherString);
Alternatively, since String is a CharSequence, something like:
for (int i=0; i<4; i++){
if (anotherString.charAt(i) != shortString.charAt(i)) return false;
}
would work as well.
Finally, every String is constructed in backing Array, there's no way to deny it, and longString.subString(0,3) would always (except index out of bound) return a String with a 4-element Char Array.
In the event that you actually need to get rid of the backing array the following will work:
String newString = StringBuilder(oldString).toString();
This might be necessary, for example, if you are parsing strings and creating substrings and you might need to do this:
String newString = StringBuilder(oldString.substring(start,end).toString();
This creates a truly new string with a zero offset and independent backing array. Otherwise, you maintain the same backing array which, in rare cases might cause a problem for the heap because it can never be garbage collected.