Error in conversion of upper and lower cases


Error in conversion of upper and lower cases



Below code is to convert upper case to lower case and vice vers?


if(s1.charAt(i)>=97 && s1.charAt(i)<=122){
s1.charAt(i)=s1.charAt(i)-32;
}
else if(s1.charAt(i)>=65 && s1.charAt(i)<=90){
s1.charAt(i)=s1.charAt(i)+32;
}



Please refer above and help what is the issue with this program?





Possible duplicate of Replace character in StringBuilder
– Shubhendu Pramanik
Jun 29 at 10:08





what is s1? a String? String is immutable, cannot be changed; anyway charAt is a method that returns a char, you cannot assign a new value to that returned value.
– Carlos Heuberger
Jun 29 at 10:15


s1


String


charAt




2 Answers
2



You have a problem here :


s1.charAt(i) = s1.charAt(i) - 32;
------------ -----------------
1 2



Here there are two problems :



Instead I would use :


String s1 = "AbCd";
//create a char array
char array = s1.toCharArray();
//loop over this array, and work just with it
for (int i = 0; i < array.length; i++) {
if (array[i] >= 'a' && array[i] <= 'z') {
array[i] = (char) (s1.charAt(i) - 32);//<--------------------------note this
} else if (s1.charAt(i) >= 'A' && s1.charAt(i) <= 'Z') {
array[i] = (char) (s1.charAt(i) + 32);
}
}

//when you end, convert that array to the String
s1 = String.valueOf(array);//output of AbCd is aBcD



Beside I would like to use :


String result = "";
for (int i = 0; i < array.length; i++) {
if (Character.isLowerCase(array[i])) {
result += Character.toUpperCase(s1.charAt(i));
} else {
result += Character.toLowerCase(s1.charAt(i));
}
}





would it be more readable to use 'a', 'z', ... instead of the integer values?
– Carlos Heuberger
Jun 29 at 10:20


'a'


'z'





exactly @CarlosHeuberger edit it thank you
– YCF_L
Jun 29 at 10:23






eventually32 = 'A' - 'a' + 1 (no so sure if that improves that much... it also needs casting, and may be confusing)
– Carlos Heuberger
Jun 29 at 10:28



32 = 'A' - 'a' + 1





this is true @CarlosHeuberger but instead I would like to use my second solution
– YCF_L
Jun 29 at 10:33





agreed, the second solution is much easier to understand
– Carlos Heuberger
Jun 29 at 11:24



Your code doesn't even compile, you can't do s1.charAt(i) = 'x', because s1.charAt is not a variable, you can't just assign anything to it.


s1.charAt(i) = 'x'


s1.charAt



To replace a character by index in a String, do:



new StringBuilder(yourString).setCharAt(characterIndex, 'x')


new StringBuilder(yourString).setCharAt(characterIndex, 'x')



I suggest using some IDE like Intellij or Eclipse, it will tell you about compile errors like those.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift