Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6722049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:24:06+00:00 2026-05-26T09:24:06+00:00

I essentially want to search the frequency of a string. For example, if I

  • 0

I essentially want to search the frequency of a string. For example, if I pass in the word “I”, then the frequency of the word in the following sentence: “I went to the beach and I saw three people” should be 2. I’ve constructed such method in which I take a text (of any length), split it into an array by the white space, and loop through the array, searching if each index matches the word. Then, I increment the frequency counter and return the number as a string. Here’s the method:

private int freq() {
String text = "I went to the beach and I saw three people";
String search = "I";
String[] splitter = text.split("\\s+");
int counter = 0;
   for (int i=0; i<splitter.length; i++)
   {
       if (splitter[i]==search) 
       {
           counter++;
       }
       else
       {

       }
   }
   return counter;
       }

}  

This is outside the method:

String final = Integer.toString(freq());
System.out.println(final);

But as I run this, I keep getting 0 as the result. I don’t know what I’m doing wrong.

EDIT: You’re all correct! What a waste of a question :(.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:24:07+00:00Added an answer on May 26, 2026 at 9:24 am

    Use equals instead of ==

    if (text[i].equals(search) )
       {
           counter++;
       }
    

    better solution

    Use a Map to map the words Map<String,Integer> with frequency.

    String [] words = line.split(" ");
    
    Map<String,Integer> frequency = new HashMap<String,Integer>();
    
    for (String word:words){
    
        Integer f = frequency.get(word);
        //checking null
        if(f==null) f=0;
        frequency.put(word,f+1);
    }
    

    Then you can find out for a particular word with:

    frequency.get(word)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Essentially what I want to do is search the working directory recursively, then use
Essentially I have 3 fields I want to check for: class Book { String
Essentially what I want to do is search a number of MYSQL databases and
I essentially want to display a screen whenever the screen is unlocked regardless of
I've gotten myself into a situation that seems fairly unusual, and essentially want to
Essentially I want a border-less, black window which I can set the location and
Essentially I want to suck a line of text from a file, assign the
I have 3 date range forms on my site. Essentially I want to hide
Essentially what I want to do is to apply additional CSS classes to individual
If I want to essentially grep every line ever in the repository, is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.