Results 1 to 2 of 2

Thread: getChar help

  1. #1
    Join Date
    Feb 2008
    Location
    Arizona
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getChar help

    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:

    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
    Guidance would be much appreciated! Thanks very much
    Last edited by davydude; 10-22-2008 at 02:15 PM.

  2. #2
    Join Date
    Oct 2008
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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.
    Last edited by Hexbomb; 10-27-2008 at 04:55 AM.

Bookmarks

Posting Permissions

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