You are getting a compiler error are because you have placed the return statement inside the for loop. It isn't grammatically incorrect to have a return statement inside a for loop (as long as it makes sense to have it there)...but you need to have a return statement after the for loop regardless.
try writing it this way
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();
} // for loop ends here
return strBuf.toString();
} //--filter method ends here.
}
Hope that helps