Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your loop is executing after entering a three-word phrase because, as Eric mentioned, a <code>'do-while'</code> loop will always execute at least once. The best, simple solution to resolve that issue would be to use a <code>while</code> loop instead, as follows:</p> <pre><code>while(wordCount != 3) { JOptionPane.showMessageDialog(null, "Error, you need to input a 3 word phrase to create a 3 letter acronym." + '\n' phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase");enter code here } </code></pre> <p>A <code>do-while</code> loop performs an action and then checks if it should perform that action again. A <code>while</code> loop checks if it should perform an action first, which is what you want to be doing.</p> <hr> <p>There are also some other significant issues that would be worth resolving at the same time.</p> <p>Your variables <code>word</code> and <code>wordCount</code> are not being updated once the program enters your <code>do-while</code> loop. If a user first enters a three-word phrase, they're fine. If they enter a phrase of any other length the program will loop indefinitely. Since the program is executing the logic inside the <code>do-while</code> loop only, and that does not include saving new words into the variable <code>word</code> or figuring their length an dstoring it in <code>wordCount</code>, there is no way to exit the loop. </p> <p>As a quick fix I added these two lines to solve those issues:</p> <pre><code>word = phrase.split("\\s+"); wordCount = word.length; </code></pre> <p>You could also eliminate the wordCount variable entirely by checking, but I'm honestly not sure if there's value in doing that versus having the variable in use. <code>phrase.split("\\s+").length</code> in the <code>while</code> loop.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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