Help - Search - Members - Calendar
Full Version: I Need Java Help
Scenyx Entertainment Community > Off Topic Forums > General Chat > "The Dump"
arena_anth
I need to develop a method that will count the number of words in a given string, and another method that will count the number of occurances of a particular word within a given string.

public class MyString {

public MyString() {
// Constructor -- do nothing.
}

// THE METHODS YOU WRITE

// This method counts the words in the String line
// words are separated by characters that are not letters or digits
public int wordCount(String line){
int count = 0;

return count;
}

// This method counts the number of times the word appears in the string line
// The method looks for the exact occurence of the word.
// The method ignores any characters that are not digits or letters
// in matching word in the string line.
public int wordFrequency(String word, String line){
int count = 0;

// Be sure to handle the error cases here.

return count;
}
}

This next code is the testing:
public static void main(String[] args) {
// TODO code application logic here

MyString s = new MyString();

int count;

// Tests of method wordCount

System.out.println("Testing method wordCount");

count = s.wordCount("This is a test");
System.out.println(count + " should be 4");

count = s.wordCount("This1 puts it 2 the test!\n 4ever");
System.out.println(count + " should be 7");

count = s.wordCount("\t \n \r");
System.out.println(count + " should be 0");

count = s.wordCount("this-is-a-test.");
System.out.println(count + " should be 4");

count = s.wordCount("");
System.out.println(count + " should be 0");


// Tests of method wordFrequency

System.out.println("\nTesting method wordFrequency");

count = s.wordFrequency("is", "This is a test");
System.out.println(count + " should be 1");

count = s.wordFrequency("number", "1number = number1");
System.out.println(count + " should be 0");

count = s.wordFrequency("fuZZy", "Fuzz-Fuzzy-wuzzy was a bear?\n");
System.out.println(count + " should be 1");

count = s.wordFrequency("\n", "MARY, marry Oh Mary!");
System.out.println(count + " should be -1");

count = s.wordFrequency("", "1number = number1");
System.out.println(count + " should be -1");
}

}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.