PDA

View Full Version : Need help with String Buffer in Java


aaronpatel
07-17-2007, 10:08 AM
Hello.

I am trying to pick up Java for a college course i will be taking next year. I have been given a few sample test questions, and one of them is as follows.

Write a method that counts the occurences 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 occurences 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.

I know this is basic for many of you, but this is only my second week of java.

TQ.

ess
07-17-2007, 08:45 PM
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

aaronpatel
07-18-2007, 02:20 AM
Well, thanks a lot.

Such a small error can just cause something not to work.

What you suggested was just spot on and it worked straight away.

Thanks a lot again.

aaronpatel
07-18-2007, 03:32 AM
mistake

aaronpatel
07-18-2007, 03:40 AM
Right after i posted this message, i figured the whole thing out.

Must have been some sort of error with putting "()" after using a method from the string class. Funny the compiler never caught it.

Anyway thanks for all your help.

This is my final program if you guys want to see what you helped me with.


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);


int[] counts = countNumbers(s1);

String output = "";

for (int i = 0; i < counts.length; i++) {
if (counts[i] != 0)
output += (char)('0' + i) + " appears " +
counts[i] + ((counts[i] == 1) ? "
time\n" : " times\n");
}

JOptionPane.showMessageDialog(null, output, "Test Exercise 7-5 The Character Class",
JOptionPane.INFORMATION_MESSAGE);
}

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();
}

private static int[] countNumbers(String s) {
int[] counts = new int[10];

for(int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i)));
counts[s.charAt(i) - '0']++;
}
return counts;
}
}