For my Homework I need to count chars in a String. If there are more than three of the same char behind each other the method schould return true if not false.
Now develop a method noMultiples (), which checks if there are three (or more) equal characters in a string
Generalize noMultiples () now,that the number of characters to be repeated is parameterized.
I alredy tried to spilt the String in to substrings but received an String index out of Bounds error. Also i tried to work with the charAt() method but I am kinda stuck now. Are there any ways to solve this problem ?
Thanks in advance
public static boolean noMultiples(int n, String s) {
for(int i=0;i<s.length();i++){
if(s.charAt(i)==s.charAt(i+1)&&s.charAt(i+1)==s.charAt(i+2)){
return true;
}
}
return false;
}
This should be the expected output :
public static void main(String[] args) {
boolean result = noMultiples("Hello World");
System.out.println(result); // => true
System.out.println(noMultiples("faaantastic")); // => false
System.out.println(noMultiples(2, "Hello World")); // => false
}