-3

removeNchars takes a String, an int and a char and returns a String: The output string is the same as the input string except that the first n occurrences of the input char are removed from the string, where n represents the input integer. If there are not n occurrences of the input character, then all occurrences of the character are removed. Do not use arrays to solve this problem.

HW2.removeNchars("Hello there!", 2, 'e') "Hllo thre!"

HW2.removeNchars("Hello there!", 10, 'e') "Hllo thr!"

HW2.removeNchars("Hello there!", 1, 'h') "Hello tere!"

public class HW2{   
    public static String removeNchars(String s, int a, char b){
       StringBuilder s = new StringBuilder();
       for(int i = 0; i<s.length(); i++){
           if(int i=a&& s.charAr(i)==b){
           }          
       }              
    }

}
Krishna Vyas
  • 1,009
  • 8
  • 25
  • You can' NOT use the variable name **s** for both your method String parameter and your StringBuilder object. Change the StringBuilder object name to **sb**. You do not want to use the variable **i** as your occurance counter, as this won't work. Use a separate counter variable for this and declare it (`int hitCounter = 0;`) above your loop. `String strgChar = Character.toString(s.charAt(i)); if (s.charAt(i) == b && hitCounter < a) { hitCounter++; strgChar = ""; } sb.append(strgChar);`. – DevilsHnd - 退職した Oct 07 '19 at 05:38
  • That's what my actual mistakes and the things I need to learn – Haomiao Shi Oct 07 '19 at 06:02

3 Answers3

0

there is more than 1 way to do it. I can see you used StringBuilder so try this

the idea is to define string builder

StringBuilder sb = new StringBuilder(inputString);

use deleteCharAt() to delete the char you don't want(docs).

and convert it back to string

String resultString = sb.toString();

good luck

Edit:

You can also create a new StringBuilder(exmple name output) then iterate the src string and append the chars you don't want to remove to the output string with .append(input);

something like this:

 StringBuilder output = new StringBuilder();
 output.append(char);

and return the output string after you finish iterating.

Dor
  • 657
  • 1
  • 5
  • 11
  • deleteCharAt() is not allowed.only allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method – Haomiao Shi Oct 07 '19 at 05:21
0

I don't know why you are using the StringBuilder class,but as you stated above i think this will help you with your homework,lets see if it works for you.

public static String removeNchars(String s, int a, char b){
       String str = "";
           for(int i = 0; i < s.length(); i++){
                if(a > 0 && s.charAt(i) == b)
                  {
                       a--;
                  }else 
                  {
                      str += s.charAt(i);
                  }

      }
      return str;

}
yafiet andebrhan
  • 161
  • 1
  • 3
  • 11
-1

You can use s.deleteCharAt(); like this:

public class HW2{   
    public static String removeNchars(String s, int a, char b){
        StringBuilder s = new StringBuilder();
        for(int i = 0; i<s.length(); i++){
            if(int i=a&& s.charAr(i)==b)
                s.deleteCharAt(i);
        }
    }
}
Elad Cohen
  • 453
  • 3
  • 16
  • I am not allowed to use deleteCharAt(). only You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character – Haomiao Shi Oct 07 '19 at 05:19