Why not the toLowerCase () method; working in my code?

import java.util.Scanner;

public class Test
{

    public static void main(String[] args)
    {
        char[] sArray;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter a Palindrome : ");

        String s = scan.nextLine();


        sArray = new char[s.length()];

        for(int i = 0; i < s.length(); i++)
        {
            s.toLowerCase();
            sArray[i] = s.charAt(i);
            System.out.print(sArray[i]);
        }

    }
}
+5
source share
2 answers

This does not work because strings are immutable. You need to reassign:

s = s.toLowerCase();

toLowerCase() returns the changed value, it does not change the value of the instance on which you call this method.

+24
source

You need to do:

String newStr = s.toLowerCase();
+4
source

All Articles