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 8221349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:53:09+00:00 2026-06-07T13:53:09+00:00

I am attempting to search a user input array of text with another user

  • 0

I am attempting to search a user input array of text with another user input array of search terms using nested loops and then output the search terms with the number of times they appear in the text along with the percentage of total text. I think I am on the right track and my issue is that the counter is not resetting each time the if statement is true. I am very new to programming — so I could be completely wrong. Below is the entire program. If anyone could take a look and give me a hand at figuring out what my issue is I would be eternally grateful.

public class termFrequency {
    public static void main(String[] args) {

        String searchTextPeriod, searchTextComma, searchTextApostrophe, searchTextColon, searchTextExclamation, 
        searchTextQuestion, searchText, searchTerm;
        int counter=0, total, searchIndex=0, termIndex=0;
        double percentage=0.0;
        String [] searchArray, termArray;


        searchText = JOptionPane.showInputDialog("Enter a sentence that is at least 20 words long");

        //removes some common punctuation from the searchable text
        searchTextPeriod = searchText.replace(".", "");
        searchTextComma = searchTextPeriod.replace(",", "");
        searchTextApostrophe = searchTextComma.replace("'", " ");
        searchTextColon = searchTextApostrophe.replace(":", " ");
        searchTextExclamation = searchTextColon.replace("!", "");
        searchTextQuestion = searchTextExclamation.replace("?", "");

        searchArray = searchTextQuestion.split(" "); //splits the sentence and and puts it into an array

        total=searchArray.length;
        System.out.println("There are " +total +" words in your sentence");

        searchTerm = JOptionPane.showInputDialog("Enter your search terms here seperated by a space");
        termArray = searchTerm.split(" ");
        DecimalFormat two = new DecimalFormat("#0.00");

        boolean found = false;
        for (termIndex=0; termIndex<termArray.length; termIndex++) 
        {
            for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 

                if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex]))
                {
                    counter++; 
                    found = true;

                    percentage= ((double) counter/(double)total) * 100;
                }
                if (found) 
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
                else  
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement.");

            }
        }
    }
}
  • 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-06-07T13:53:12+00:00Added an answer on June 7, 2026 at 1:53 pm

    You have to move the if/else on “found” from the inner loop to the end of the first loop.
    You also need to reset the boolean and the counter in the first loop, like that you start the analysis of each new word in termArray with initial values.

       for (termIndex=0; termIndex<termArray.length; termIndex++) 
        {
            counter=0; //Reset the counter for each word in termArray
            found=false; //Reset the "found" flag for each word in termArray
            for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 
    
                if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex]))
                {
                    counter++;
                    percentage= ((double) counter/(double)total) * 100;
    
                    found=true
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
                }
            }
            if (found) 
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
            else  
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement.");
    
        }
    

    By the way you don’t really need the “found” var, now if counter == 0 you know that the word has not been found in searchArray.

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

Sidebar

Related Questions

I am attempting to use a text field to search for a user email
I am attempting to use the following code to search a database using words
I am attempting to create a search function for user profiles on my site.
I am attempting to make a program that will input numbers into an array
I am attempting to allow a user to type in their search query in
I'm attempting to create a search function, built something like this: function Search(array, key,
I'm attempting to construct a search query in MySQL using multiple arguments, and I
I'm attempting to authenticate a user against ADAM using a user I created in
I am attempting to use a recursive function to search through a multidimensional array,
We are using Solr for it's full text search capability, lets say we are

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.