简体   繁体   中英

I need to be able to enter a string and find the number of occurrences of a specific word

My instruction for this was to write a program that prompts the user to enter a string and count the number of occurrences of the word: fox. it always comes up with the error

Main.java:23: error: bad operand types for binary operator '==' 
            if (str.charAt(i) == a)
                              ^
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println("Enter a string: ");
        String str = input.nextLine();

        System.out.println("Enter a word: ");
        String a = input.nextLine();
        int letterCheck = count(str, a);

        System.out.println("The word " + a + "appeared" + letterCheck + "times in" + str);
    }

    public static int count(String str, String a)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) == a)
            {
                count++;
            }
        }
        return count;
    }
}

The following won't work for several reasons.

  • you are trying to compare a string to a character.
  • even if that worked, you are not counting the words.
   public static int count(String str, String a)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) == a)
            {
                count++;
            }
        }
        return count;
    }
}

Try this. It works by searching for the index of the word in the string. Each time is starts off from one point beyond where the last word was found. When -1 is returned, now more words can be found.

public static int count(String str, String a) {
   int count = 0;
   int i = -1;
   str = str+ " "; // in case the word is at end of sentence.
   while ((i = str.indexOf(a,i+1)) >= 0) {
      count++; 
   }
   return count;    
}

You have a problem with the function you made, you cant compare string to a char, and you write on the question " count the number of occurrences of the word " and not " count the number of occurrences of the char ", even if you want to compare a char to string you cant do this like this and need use with .equals() .

Remember when you compare String to char it will not compare the contents of the string to a character, it will compare the object's location in memory, resulting in unwanted results.

You cant fix it with the answers to this answer: Occurrences of substring in a string

And the output of the function:

Enter a string: 
njnkjhellonkjnjknjkhellol;,l;,;lhellohellomklmlkmhellomlkmklmklhellomklmklmhello
Enter a word: 
hello
The word hello appeared 7 times in njnkjhellonkjnjknjkhellol;,l;,;lhellohellomklmlkmhellomlkmklmklhellomklmklmhello


Enter a string: 
545642123worldmklmklmklworld5465456world$@#%$#@^#$worldnjknjknjworld
Enter a word: 
world
The word world appeared 5 times in 545642123worldmklmklmklworld5465456world$@#%$#@^#$worldnjknjknjworld

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM