I'm trying to create a short program that would convert all letters that are uppercase to lowercase (from the command line input).
The following compiles but does not give me the result I am expecting. What would be the reason for this??
Eg) java toLowerCase BANaNa -> to give an output of banana
public class toLowerCase{
public static void main(String[] args){
toLowerCase(args[0]);
}
public static void toLowerCase(String a){
for (int i = 0; i< a.length(); i++){
char aChar = a.charAt(i);
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
System.out.print(a);
}
}
}
You should print
newStr
outside for loop. You were trying to print it inside the loopIf somebody needs clear code without MagicNumbers and as less as possible conversions here is my solution:
Sorry pretty late to the scene but this should solve it. An else condition because when it is not zero it totally discards the alphabet.
Looks like homework to me, Just a hint. You are printing string
a
whereas you are modifying thechar
typeaChar
, its not modifying the original stringa
. (Remember strings are immutable).