Tuesday, July 12, 2011

Assignment 4 Exercise 6

/************************************
*T'Nora Green
*Assignment 4, Exercise 6
*************************************/
import java.util.Scanner;

public class CountSubstringOccurrences
{
public static void main(String[] args)
{
Scanner stdIn= new Scanner(System.in);
String songs =
" 1. Green Day - American Idiot\n" +
" 2. Jesus Jones - Right Here, Right Now\n" +
" 3. Indigo Girls - Closer to Fine\n" +
" 4. Peter Tosh - Equal Rights\n";

String searchText;  // text that is searched for
int foundIndex=0; // position of where text is found
int count = 0; // number of occurences of search text

System.out.print("Enter search text: ");
searchText= stdIn.nextLine();

while(foundIndex <= songs.length())
{
foundIndex += songs.indexOf(searchText, foundIndex);
count++;
} //end loop

System.out.println("Number of occurences of \"" + searchText + "\": " + count);

} //end main
} //end class CountSubstringOccurrences

No comments:

Post a Comment