
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.39 KB | hits: 14 | expires: Never
//the very best pokemon trainer
import java.util.Scanner;
public class PhraseCounter {
public static void main(String args[]) throws java.io.IOException {
Scanner keyboard = new Scanner(System.in);
boolean isDone = false;
String search;
int count = 0;
String temp;
String space = " ";
System.out
.println("Enter some text to search in. Enter a blank line to stop.");
temp = keyboard.nextLine();
while (isDone == false) {
String word = keyboard.nextLine();
if (word.length() == 0)
isDone = true;
temp = temp + space + word;
}
System.out.println("Enter a phrase to search the above text for: ");
search = keyboard.nextLine();
while(search.length() == 0){
System.out.println("Please enter a non-blank phrase: ");
search = keyboard.nextLine();
}
search = search.toLowerCase();
search = search + space;
temp = temp.toLowerCase();
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == search.charAt(0)) {
for (int j = 0; j < search.length(); j++) {
if (temp.charAt(i + j) != search.charAt(j)) {
break;
} else if (j == search.length() - 1) {
count++;
}
}
}
}
if (count == 1)
System.out.print("The phrase " + search + " was found " + count
+ " time.");
else
System.out.print("The phrase " + search + " was found " + count
+ " times.");
}
}