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