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

  • Home
  • SEARCH
  • 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 3691768
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:11:06+00:00 2026-05-19T04:11:06+00:00

I have a couple of functions that I know work correctly. One just counts

  • 0

I have a couple of functions that I know work correctly. One just counts the number of words in a textbox using a regular expression, and the other also uses a regex to count the instances of a particular word (string) in the same textbox. I’ve made sure that these both return the values expected.

However, I’m obviously screwing up the function to populate my datagrid somehow, because the keywordcount always returns as zero, as does the density.

Here’s my code:

public List<KeywordDensity> LoadCollectionData()
{
   string thearticle = txtArticle.Text;
   string[] keywordsarray = txtKeywords.Text.Split('\n');

   bool isincluded = false;
   int keywordcount = 0;
   int thedensity = 0;

   List<KeywordDensity> lsikeywords = new List<KeywordDensity>();
   foreach (string s in keywordsarray)
   {
      if (s.Trim() != "")
      {
         keywordcount = KeywordCount(thearticle, s);
         thedensity = keywordcount / WordCount(thearticle);

         if (thearticle.Contains(s))
         {
            isincluded = true;
         }
         else
         {
            isincluded = false;
         }

         lsikeywords.Add(new KeywordDensity()
         {
            included = isincluded,    
            keyword = s,
            occurences = keywordcount.ToString(),
            density = thedensity.ToString()
         });
      }
   }
   return lsikeywords;
}

EDIT @ 8:30AM MST: Figured out part of the issue. Using “\n” as the split character wasn’t working. Apparently it was simply “splitting” the entire thing into one massive chunk that included the carriage returns and submitting that. I changed it to “\r” and now at least the “Included” and “Occurences” part works fine.

Now the only problem I’m having is that the “density” isn’t working. I realize it can’t be declared as an int; but it still always returns 0 – even when I declare thedensity as a float or var.

Is there something wrong with the following?

thedensity = keywordcount / wordcount;

Also – does the .ToString() function truncate the decimal point plus everything past it? For instance, if the value of a variable it 0.43 it would just convert to 0? If that’s not the case, what variable type should I be using? I would think that float would be appropriate.

Thanks again!

EDIT @ 8:45 MST: Too bad I can’t accept multiple answers. Both the ‘\r’ and the var types were issues. I hadn’t realized that you can’t calculate a float by dividing two integers. When I changed the source variables to floats as well as having the density var it as well it worked.

Double thanks!

-Sootah

  • 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-19T04:11:06+00:00Added an answer on May 19, 2026 at 4:11 am

    You’re calculating with integers. WordCount sounds to me as a big number, keywordcount not. Maybe that’s why you have not the result you expect?

    Another question, what if WordCount returns zero? This will throw an exception. Does this never happen (assume not, but does it?)? If the exception will be thrown in binding, the app will not hang but the result will be false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of functions inside a class that essentially do the same
I have some code in a couple of different functions that looks something like
I have a C program with a couple of static functions that are not
I have couple resource DLLs that I currently load when application starts using following
I have a couple of linux (ubuntu) servers that work together every night to
Im using the bassistance form validation. I have a couple of fields that require
I have a couple functions like this: object obj.getChild(childIndex) int obj.numChildren() So I am
I have the following couple of C pre-processor macros for creating test functions: //
I have couple of dozen pieces of data that I need to save and
I have a program that uses windows messages to know if a button is

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.