| | |
"String class"
![]() |
•
•
Join Date: Jul 2004
Posts: 2
Reputation:
Solved Threads: 0
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
"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
Ok , you want something like this:
Class StringProg has the 2 methods you require:
---------------------------------------------------------------
public class StringProg
{
String s;
int count = 0;
//constructor
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
t.displayString();
//search the string for a letter
t.findChar(letter);
}
---------------------------------------------------------
That should help ya. rickste_r!
Class StringProg has the 2 methods you require:
---------------------------------------------------------------
public class StringProg
{
String s;
int count = 0;
//constructor
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
t.displayString();
//search the string for a letter
t.findChar(letter);
}
---------------------------------------------------------
That should help ya. rickste_r!
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
![]() |
Similar Threads
- Something about String.split("-"); problem (Java)
- error in user defined string class (C++)
- String Class Of Java (Java)
Other Threads in the Java Forum
- Previous Thread: string translation
- Next Thread: Error message when running simple Java pgm
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





