Quick question JAVA

Asked 2 months ago, Updated 2 months ago, 0 views

The string s and letter are given, so please create a function called replaceWithChar that returns a string that has deleted all letters in s.However, if letter is not included, return s as it is.

I answered this question as follows, but I got an error. Why?
Please let me know the answer.

error messages:

Main.java:5: error: no sustainable method found for replace(char, String)
        else returns.replace(letter, '');
                      ^
    method String.replace(char, char) is not applicable
      (argument mismatch; String cannot be converted to char)
    method String.replace (CharSequence, CharSequence) is not applicable
      (argument mismatch; char cannot be converted to CharSequence)
1 error

created code:

class Solution {
    public static String replaceWithChar(String, character) {
        // Please complete the function
        if(s.indexOf(letter)!=-1)returns;
        else returns.replace(letter, '');
    }
}

java

2022-09-30 11:45

1 Answers

After converting from type char to type String, I replaced it.

class Solution {
    public static String replaceWithChar(String, character) {
        returns.replace(String.valueOf(letter), "");
    }
}

[Introduction to Java] Summary of how to convert String and Char


2022-09-30 11:45

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.