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);
}
}
}
The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.
The toLowerCase operator takes a string and converts it to all lower case letters. The toUpperCase operator takes a string and converts it to all uppercase letters.
ToUpper depends on what your strings contain more of, and that typically strings contain more lower case characters which makes ToLower more efficient.
To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.
You are printing the String
a
, without modifying it. You can print char directly in the loop as follows:
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(aChar);
}
}
}
Looks like homework to me, Just a hint. You are printing string a
whereas you are modifying the char
type aChar
, its not modifying the original string a
. (Remember strings are immutable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With