PDA

View Full Version : getChar help



davydude
10-22-2008, 12:01 PM
I've just started to learn JAVA, and I recentley had a piece of homework to create an array to find & count a certain letter from a textfield, say for example 'f', and then display the result.

I haven't really had much practice with array's, so i'm a little lost with how to layout this code let alone do this method.

Currently here is my code:



import java.applet.*;
import java.awt.*;
import java.lang.*;


public class VowelCount extends Applet implements ActionListener
{

Label LabelA;
TextField TextFieldA;

public void init();
{

TextFieldA = new TextField(12);
LabelA = new Label ("Enter a sentence or word: ");

add(LabelA);

add(TextFieldA);

TextFieldA.addActionListener(this);

}

public final class Array
{

getChar[] (




Then i just get a little stuck with what to do next :confused:
Guidance would be much appreciated! Thanks very much

Hexbomb
10-27-2008, 04:23 AM
Hey I have also just started my uni java courses and I wrote you a class to help you, as im not sure as the rules relating to doing others "homework".

The class has one method and returns the number of instances of the specified character from a given string.

Class CharCounter

methods:
countChar(string, char)

returns:
Int

The code:


public class CharCounter
{

/* Creates a new instance of CharCounter */
public CharCounter()
{
}

public int countChar(String source, char charToGet)
{
int counter = 0;

char []arrayOne = source.toCharArray();

for(int i=0; i <= arrayOne.length -1; i++)
{
if(arrayOne[i] == charToGet)
{
counter++;
}
}

return counter;
}
}


You can get the string from a textfield ect by using textfield.getText() and I guess the char from a text field with one character in it.

Hope it helps.