NoobCoder
NoobCoder

Reputation: 167

Convert uppercase string to lowercase using charAt

I was wondering what is exactly wrong with the following code. I'm getting error on the line after the if statement. This code takes a string with both uppercase and lowercase letters but returns the string after converting the uppercase letters to lowercase.

public class Main {

    public static void main(String[] args) {
        toLowerCase("HeLloWoRlD!");
    }

    private static String toLowerCase(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                str.charAt(i) = Character.toLowerCase(str.charAt(i));
            }
        }
        return str;
    }

}

Upvotes: 0

Views: 1833

Answers (8)

0day
0day

Reputation: 103

private static String toLowerCase(String str){

    return str.chars().map(Character::toLowerCase).collect(StringBuilder::new,
            StringBuilder::appendCodePoint, StringBuilder::append)
            .toString();

}

or

private static String toLowerCase(String str){

    return str.toLowerCase();

}

Upvotes: 0

Nik.exe
Nik.exe

Reputation: 411

String is immutable so you cannot change (reassign) the characters inside the string. Here is the simplest solution, just using the built in method in String class:

private static String toLowerCase(String str) {
    return str == null ? null : str.toLowerCase(); //consider str.toLowerCase(Locale.ROOT) if you are using non english language with special characters
}

Upvotes: 1

Navin Kumar
Navin Kumar

Reputation: 1

First take a variable the assign value on it. This code will work as expected

public class Main {

    public static void main(String[] args) {
        System.out.println(toLowerCase("HeLloWoRlD!"));
    }

    private static StringBuffer toLowerCase(String str) {
        StringBuffer buf=new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            if (Character.isUpperCase(str.charAt(i))) {
                c = Character.toLowerCase(str.charAt(i));
                buf.append(c);
            }else{
                buf.append(c);
            }
        }
        return buf;
    }
}

Upvotes: 0

Flow
Flow

Reputation: 791

You cannot change the content of a String. You have to create a new object instead:

public class Main {

    public static void main(String[] args) {
        toLowerCase("HeLloWoRlD!");
    }

    private static String toLowerCase(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                str = new String(str.replace(str.charAt(i),Character.toLowerCase(str.charAt(i))));
            }
        }
        return str;
    }

}

Upvotes: 0

BigButovskyi
BigButovskyi

Reputation: 129

The method charAt(i) is used for getting character by index(position) in the string. However, You used it for setting, what is not right.

So, in your case, method toLowerCase() should return new String object, for example.

private static String toLowerCase(String str) {
  String returning_Str = ""; 
    for (int i = 0; i < str.length(); i++) {
        char test_char = str.charAt(i);
        if (Character.isUpperCase(test_char)) {
          test_char = Character.toLowerCase(test_char);
        }
        returning_Str += test_char;
    }
    return returning_Str;
}

Upvotes: 0

Renny
Renny

Reputation: 100

You are making use of the String.charAt(i) which returns character Value at that position. It is not the reference location that you can assign a value to.

Please check the below documentation. https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-

You need to convert to a character Array the string if you want to modify it using the charAt feature

private static String toLowerCase(String str) {
        char[] newStr = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                newStr[i] = Character.toLowerCase(str.charAt(i));
            }
        }
        return new String(newStr);
    }

Upvotes: 0

Nicholas K
Nicholas K

Reputation: 15443

Change you code to the following :

private static String toLowerCase(String str) {
    StringBuffer lower = new StringBuffer();
    for (int i = 0; i < str.length(); i++) {
        if (Character.isUpperCase(str.charAt(i))) {
            lower.append(Character.toLowerCase(str.charAt(i)));
        } else {
            lower.append(str.charAt(i));
        }
    }
    return lower.toString();
}

public static void main(String[] args) throws IOException {
    System.out.println(toLowerCase("HeLloWoRlD!"));
}

A string is immutable so you can't change the existing one on the fly. Instead you can create a StringBuffer and append the values accordingly as you iterate over the original str.

Upvotes: 1

Eran
Eran

Reputation: 394126

str.charAt(i) cannot be the left hand side of an assignment operator. It's a value returned by a method call, you can't assign to it.

Besides, Strings are immutable. You cannot modify the characters of str. You'll have to create a new String for your method to return.

For example:

private static String toLowerCase(String str) {
    StringBuilder sb = new StringBuilder(str.length());
    for (int i = 0; i < str.length(); i++) {
        if (Character.isUpperCase(str.charAt(i))) {
            sb.append (Character.toLowerCase(str.charAt(i)));
        } else {
            sb.append (str.charAt(i));
        }
    }
    return sb.toString();
}

Upvotes: 6

Related Questions