write and test a class that can be used to create and object whose instance string variable can be initialised to a string constant such as:
"I fined that the harder I work the more luck I seem to have"

the class need to provide the following methods

A method to diplay an object's string.
A method that given a character, it returns the number of times that characte occurs in the string within the object.
eg: the character 'e' occurs 7 times within the sample string.

Please help me with this

12 Years
Discussion Span
Last Post by Iron_Cross

Ok , you want something like this:

Class StringProg has the 2 methods you require:

public class StringProg

    String s;
    int count = 0;

    public StringProg(String sIn)
        s  = sIn;

    //method to diplay string
    public void displayString()
        System.out.println ("\nYour String: " + s); 

    //method to find character
    public void findChar(char letter)

        for (int i = 0; i<s.length(); i++) 
            if(s.charAt(i) == letter)
                count ++;
        System.out.println ("\nYour character " + "\"" + letter 
        + "\"" + " appeared " + count + " time(s)");    


The second class tests the methods from StringProg:

public static void main(String [] args)
        //the requested letter to find
        char letter = 'e';

        //create a StringProg object
        StringProg t = new StringProg("THE STRING GOES HERE");

        //display the string
        //search the string for a letter


That should help ya. rickste_r!

Edited by Dani: Formatting fixed


Hello rickste_r

Thankyou very much for the help. It was very kind of you.



Don't give out homework for free! They're never going to learn anything if you do it for them. There is nothing wrong with helping them, but DON'T do it for them!
And also, use the code tags around your code please

Thank you.

Edited by happygeek: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.