View Single Post
  #1 (permalink)  
Old 08-07-2007, 02:35 AM
mathias mathias is offline
Member
 
Join Date: Jul 2007
Posts: 40
mathias is on a distinguished road
Help with String Buffer
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:

Code:
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:
Code:
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.
Reply With Quote
Sponsored Links