Problem Sanket and String---

what’s the error in my code----
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int k = cin.nextInt();
String str = cin.nextLine();
int counta = 0, countb =0;
for(int i = 0;i<str.length();i++){
if(str.charAt(i) == ‘a’){
counta++;
}else{
countb++;
}
}
int count = 0;
char ch1 = ‘a’, ch2 = ‘b’;
if(counta>countb){
while(count<=k){
for(int i = 0;i<str.length();i++){
if(str.charAt(i) == ‘b’){
str.charAt(i) = ch1;
count++;
}
}
}
}else{
while(count<=k){
for(int i = 0;i<str.length();i++){
if(str.charAt(i) == ‘a’){
str.charAt(i) = ch2;
count++;
}
}
}
}
System.out.println(str);

}
}

Hi @AbhishekAhlawat1102,
In lines str.charAt(i) = ch1; and then again in str.charAt(i) = ch2; You are trying to change the values of char at a position in a string. This will give you an error.
Because, strings in Java are immutable. You cannot edit the value of a string in Java. If you want to edit a string, use StringBuilder.