Hello,
I have recently completed a homework assignment where I had to code for someone to enter a sentence and terms to search for in that sentence. The output then tells you if the term was found in the sentence, how many times it occurs and the percentage that term occurs in the sentence. I have it working just fine via the console (using Eclipse), but (mostly just for fun) I wanted to use a dialog box because I think it looks better and I want to use it more for future assignments. The input works fine using JOptionPane.showInputDialog but at the end I have an if-else statement and since that statement may produce multiple outputs, I'm not sure how to code the JOptionPane.showMessageDialog to show all the possible outputs from the if-else statement. I can get it to show a new dialog box for each if statement that is true but I would like to have them all in one box, each on a new line (word 'x' occurs 3 times 14%; word 'y' occurs once 8%; word 'z' does not occur 0%, There are 10 words in the sentence). I'm sure this is possible, just haven't been able to figure it out on my own. Any help would be great as I'd like to do this more in future assignments.
Here is the code for the if-else statements currently using system.out.println():
if (termCount >= 0) {
double percentOccurs = termCount/coun *100;
DecimalFormat percentTwoDecimals = new DecimalFormat("0.00");
if ((termCount > 0) && (termCount < 2))
System.out.println("\'" + termArray[i] + "\' occurs once " + percentTwoDecimals.format(percentOccurs) + "%");
else if (termCount >= 2) {
int newTermCount = (int) termCount;
System.out.println("\'" + termArray[i] + "\' occurs " + newTermCount + " times " + percentTwoDecimals.format(percentOccurs) + "%"); }
else if (termCount < 1)
System.out.println("\'" + termArray[i] + "\' does not occur 0.00%");
}
}System.out.println("There are " + sentenceArray.length + " words in the this sentence.");
Thanks!
Molly