开发者

error in replaceAll() method

i have error in String st=str.replaceAll(" ",""); But include the import java.lang.String.*; After adding this line again same error repeated.. So please anyone help me to solve th开发者_StackOverflow中文版e error..

String st=str.replaceAll(" ","");  


We can't really tell what's wrong without you telling us the error message or giving us more code. Your snippet is missing a semi-colon, and relies on str being a definitely-assigned variable of type String, but that's all.

Sample code that works:

public class Test {
    public static void main(String args[]) {
        String str = "hello world";
        String st = str.replaceAll(" ", "");
        System.out.println(st); // helloworld
    }
}

Now you just need to find the difference between your code and my code...


If I understood you properly, you used the line twice, like this:

// in scope of some method:
String st=str.replaceAll(" ","");
//....
String st=str.replaceAll(" ","");

This is not legal, because you declared a variable with the same name in the same scope twice, instead, it should be:

// in scope of some method:
String st=str.replaceAll(" ","");
//....
st=str.replaceAll(" ","");

or:

// in scope of some method:
String st=str.replaceAll(" ","");
//....
String st1=str.replaceAll(" ","");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜