wam090
wam090

Reputation: 2873

Why isn't the method toLowerCase(); 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]);
        }

    }
}

Upvotes: 4

Views: 20667

Answers (2)

Brian
Brian

Reputation: 6450

You need to do:

String newStr = s.toLowerCase();

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1039170

It doesn't work because strings are immutable. You need to reassign:

s = s.toLowerCase();

The toLowerCase() returns the modified value, it doesn't modify the value of the instance you are calling this method on.

Upvotes: 27

Related Questions