Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching a string arrays with an array
    primarykey
    data
    text
    <p>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 <em>think</em> 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. </p> <pre><code>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&lt;termArray.length; termIndex++) { for (searchIndex=0; searchIndex&lt;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."); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload