6

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);
            }
         }   
    }
Vhndaree
  • 594
  • 1
  • 6
  • 20
Betty Jones
  • 73
  • 1
  • 3
  • 12
  • you had printed **a** which is same as you passed as argument change `System.out.print(a);` to `System.out.print(aChar);` – Vhndaree Nov 15 '18 at 06:53

10 Answers10

8

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);
         }
     }
}    
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
  • 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 Sep 02 '19 at 05:52
2

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

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Hm. So should I create a new empty string into which I add the new char types ? – Betty Jones Apr 28 '13 at 03:29
  • @BettyJones, yes that could be one way, or you can just print the newely created character, since you are not returning the string. – Habib Apr 28 '13 at 03:31
2

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)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 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 Sep 02 '19 at 05:54
1
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

StarsSky
  • 6,721
  • 6
  • 38
  • 63
Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45
1

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.

Robert Bolton
  • 667
  • 3
  • 8
  • 22
1
/**
     * Method will convert the Lowercase to uppercase
     * if input is null, null will be returned
     * @param input
     * @return
     */
    public static String toUpperCase(String input){
            if(input == null){
                return input;
            }
            StringBuilder builder = new StringBuilder();
            for(int i=0;i<input.length();i++){
                char stringChar = input.charAt(i);

                if(92 <= stringChar && stringChar <=122){
                    stringChar = (char)( (stringChar - 32) ); 
                    builder.append(stringChar);
                }
                else if (65 <= stringChar && stringChar<=90)
                {
                    builder.append(stringChar);
                }
            }
            if(builder.length() ==0){
                builder.append(input);
            }
            return builder.toString();
        }
0
public class Changecase
{
    static int i;

    static void changecase(String s)
    {
        for(i=0;i<s.length();i++)
        {
            int ch=s.charAt(i);
            if(ch>64&&ch<91)
            {
                ch=ch+32;
                System.out.print( (char) ch);
            }
            else if(ch>96&&ch<123)
            {
                ch=ch-32;
                System.out.print( (char) ch);
            }
            if(ch==32)
            System.out.print(" ");
        }
    }

    public static void main (String args[])
    {

        System.out.println("Original String is : ");
        System.out.println("Alive is awesome ");
        Changecase.changecase("Alive is awesome ");

    }
}
Nissa
  • 4,636
  • 8
  • 29
  • 37
  • 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. – Graham Asher Feb 23 '17 at 17:25
0
public class MyClass
{
    private String txt;
    private char lower;
    public MyClass(String txt)
    {
        this.txt = txt;
    }
    public void print()
    {
        for(int i=0;i<txt.length();i++)
        {
            if('A' <= txt.charAt(i) && txt.charAt(i) <= 'Z')
            {
                lower = (char)(txt.charAt(i) + 32);
                System.out.print(lower);
            }
            else
            {
                lower = txt.charAt(i);
                System.out.print(lower);
            }
        }
    }
    public static void main(String[] args)
    {
        MyClass mc = new MyClass("BaNaNa");
        mc.print();
    }
}

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.

0

If somebody needs clear code without MagicNumbers and as less as possible conversions here is my solution:

final char[] charArray = new char[string.length()];
for (int i = 0; i < string.length(); i++) {
    char c = string.charAt(i);
    charArray[i] = Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c);
}
String.valueOf(charArray);
VKostenc
  • 1,140
  • 14
  • 19
-3
import java.util.Scanner;
public class LowerToUpperC {

    public static void main(String[] args) {

         char ch;
            int temp;
            Scanner scan = new Scanner(System.in);

            System.out.print("Enter a Character in Lowercase : ");
            ch = scan.next().charAt(0);

            temp = (int) ch;
            temp = temp - 32;
            ch = (char) temp;

            System.out.print("Equivalent Character in Uppercase = " +ch);

    }

}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • 1
    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. – Michael Lihs Jan 10 '17 at 18:04