Hello, I have been given a few sample test questions, and one of them is as follows.
Write a method that counts the occurrences of each digit in a string using the following header:
public static int[] count (String s)
The method counts how many times a digit appears in the string. The return value is an array of ten elements each of which holds the counts for a digit.
I have done one with said criteria for Strings to count occurrences of characters, but for a combination, I am trying to figure out how to clean out the characters and leave just the integers.
I used the following in an attempt:
import javax.swing.JOptionPane;
public class mainCount {
public static void main(String[] args) {
String s = JOptionPane.showInputDialog(null, "Enter a string with numbers: ", "Test Q 7-5", JOptionPane.QUESTION_MESSAGE);
String s1 = filter(s);
}
public static String filter(String s) {
StringBuffer strBuf = new StringBuffer();
for(int i = 0; i < s.length(); i++) {
if(Character.isDigit(s.charAt(i))) {
strBuf.append(s.charAt(i));
}
return strBuf.toString();
}
}
}
I keep getting an error that the method (filter) must return a String type.
From what I have learnt isn't it doing just that, having the numbers of the entered string back as a string.
It would be greatly appreciated if anyone could help me.
Thanks.