Java - Convert lower to upper case without using toUppercase()

68,540

Solution 1

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).

Solution 2

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);
         }
     }
}    

Solution 3

A cleaner way of writing this code is

public static void printLowerCase(String a){
    for(char ch: a.toCharArray()) {
       if(ch >= 'A' && ch <= 'Z')
          ch += 'a' - 'A';
       System.out.print(ch);
    }
}

Note: this will not work for upper case characters in any other range. (There are 1,000s of them)

Solution 4

Looks like you're close. :)

For starters...

char aChar = a.charAt(i);

"a" is an array of Strings, so I believe you would want to iterate over each element

char aChar = a[i].charAt(0);

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.

System.out.print(aChar);

not

System.out.print(a);

Hope that helps you.

Solution 5

public static void toLowerCase(String a){

    String newStr = "";

    for (int i = 0; i< a.length(); i++){

        char aChar = a.charAt(i);
        if (65 <= aChar && aChar<=90){
            aChar = (char)( (aChar + 32) ); 
        }
        newStr = newStr + aChar;    
    }
    System.out.println(newStr);
}

You should print newStr outside for loop. You were trying to print it inside the loop

Share:
68,540
Betty Jones
Author by

Betty Jones

Updated on November 15, 2020

Comments

  • Betty Jones
    Betty Jones over 3 years

    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);
                }
             }   
        }
    
  • Betty Jones
    Betty Jones about 11 years
    Hm. So should I create a new empty string into which I add the new char types ?
  • Habib
    Habib about 11 years
    @BettyJones, yes that could be one way, or you can just print the newely created character, since you are not returning the string.
  • Michael Lihs
    Michael Lihs over 7 years
    please check your formatting and add some explanation. The answer does not provide the functionality that the author of the question asked for since it only converts a single character.
  • Graham Asher
    Graham Asher about 7 years
    This code doesn't change case in the string, but just prints changed-case characters. It fails to print anything for characters that are not letters or spaces. It doesn't handle non-ASCII characters at all. The reason the questioner's code fails is that it should subtract 32, not add 32, to characters in the range 65...90.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 4 years
    Is there any utility like apache/google api available for this? I want to convert case for char[] / CharSequence without using String in between. Kindly help.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 4 years
    Is there any utility like apache/google api available for this? I want to convert case for char[] / CharSequence without using String in between. Kindly help.