开发者

Why isn't the method toLowerCase(); working in my code?

开发者 https://www.devze.com 2023-01-30 08:44 出处:网络
import java.util.Scanner; public class Test { public static void main(String[] args) { char[] sArray; Scanner scan = new Scanner(System.in);
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开发者_JAVA百科 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]);
        }

    }
}


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.


You need to do:

String newStr = s.toLowerCase();
0

精彩评论

暂无评论...
验证码 换一张
取 消