I'm trying to write a code that will count the number of occurrences of 1 string in another string. So for if the user enters "hello" and then enter "e" the code should say "There is 1 occurrence of "e". However, my current executes an infinite loop.
I've tried changing the condition on the for loop to inputEntry.equals(inputCharacter)
but also had an infinite loop.
package charcounter;
import java.util.Scanner;
public class CharCounter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputEntry;
String inputCharacter;
System.out.println("Please enter a multi word string: ");
inputEntry = scnr.nextLine();
System.out.println("Enter another string: ");
inputCharacter = scnr.nextLine();
if (inputCharacter.length() == 1){
while (inputEntry.contains(inputCharacter)){
int occurrences = 0;
for(occurrences = 0;inputEntry.contains(inputCharacter); occurrences++ ){
System.out.println("There is " + occurrences + " of " + inputCharacter);
}
}
}
else{
System.out.println("Your string is too long.");
}
}
}
So for if the user enters "hello" and then enter "e" the code should say "There is 1 occurrence of "e".