Results 1 to 2 of 2
  1. #1
    mathias is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default 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:

    Java 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:
    Java 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.

  2. #2
    baltimore is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    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
    Java 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();
    } // for loop ends here
    return strBuf.toString();
    } //--filter method ends here.
    }
    Hope that helps

Similar Threads

  1. [SOLVED] Save the current buffer to file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 08:46 AM
  2. Replies: 0
    Last Post: 03-11-2008, 01:25 PM
  3. Trouble with Buffer Sizing
    By Jeff in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 02:43 PM
  4. Http Buffer issue... chunked?
    By jlew in forum Networking
    Replies: 1
    Last Post: 11-04-2007, 06:45 PM
  5. how to set the value of BUFFER SIZE
    By oregon in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 04:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •