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 are printing the
String
a
, without modifying it. You can print char directly in the loop as follows:A cleaner way of writing this code is
Note: this will not work for upper case characters in any other range. (There are 1,000s of them)
Looks like you're close. :)
For starters...
"a" is an array of Strings, so I believe you would want to iterate over each element
and it also seems like you want to return the value of the modified variable, not of "a" which was the originally passed in variable.
not
Hope that helps you.