-4

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".

recnac
  • 3,744
  • 6
  • 24
  • 46
irv528
  • 1
  • 1
  • 4
    When did you expect the loop to end? Why? – shmosel Apr 28 '19 at 23:08
  • 3
    Your program is saying "while the inputEntry contains inputCharacter keep looping". How do you expect this not to be an infinite loop? Both the `while` and the `for` loops will never end. – jbx Apr 28 '19 at 23:09
  • I wanted the loop to end once it was done counting the number of occurrences. So if the word was "Good" and the second string entered was "g" the loop would read the word good and see g came up only once and then stop and report 1 occurrence. – irv528 Apr 28 '19 at 23:11
  • I'm new to Java so I'm trying to go through all the resources I have available to me and what I was taught up until now. – irv528 Apr 28 '19 at 23:12
  • 1
    Yes, but `contains` checks the whole string. Read the Javadoc! https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence- – jbx Apr 28 '19 at 23:12
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Apr 28 '19 at 23:14

1 Answers1

0

The inputEntry.contains(inputCharacter) in your code always return true => endless loop

You can change to indexOf for your requirement.

int lastIndex = 0;
int count = 0;

while(lastIndex != -1){

    lastIndex = inputEntry.indexOf(inputCharacter,lastIndex);

    if(lastIndex != -1){
        count ++;
        lastIndex += inputCharacter.length();
    }
}

You can change your code to

 if (inputCharacter.length() == 1){
      int lastIndex = 0;
      int count = 0;

      while(lastIndex != -1){

        lastIndex = inputEntry.indexOf(inputCharacter,lastIndex);

        if(lastIndex != -1){
            count ++;
            lastIndex += inputCharacter.length();
        }
       }
       System.out.println("There is " + count + " of " + inputCharacter);
   }
   else{
            System.out.println("Your string is too long.");
   }
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Incorrect call to [`indexOf(String str, int fromIndex)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-java.lang.String-int-) – Andreas Apr 28 '19 at 23:17
  • What is `str`?? – Andreas Apr 28 '19 at 23:18
  • Thanks for assist but we haven't been taught indexof in our class yet. Is there any way to use the structure I have? Plus if I were to add a print out statement likeSystem.out.println("There is " + count + " of " + inputCharacter); it would print corrrently for 1 character but for more than one occurence it would print out as there is 1 of 2. – irv528 Apr 28 '19 at 23:24