title: Declaring String Variables within a while loop without it looping all throughout - java
body: <p>I am stuck at a part where I am supposed to declare a string variable called &quot;phrase&quot;, where it shouldn't loop, all the way through.</p>
  <p>to give you an idea my task is: Similar to Option 1 except the user enters 'N' (instead of 'Q') when they are done entering results for the first team.  Then, the program inputs a second team name and its results until 'Q' is entered.  Outputs two statements, like the statements in option 1 followed by a third statement that says which team is in first place (based on the number of points)</p>
  <p>Sample input:</p>
  <pre><code>2
  Toronto
  W
  W
  L
  O
  W
  O
  W
  N
  Montreal // how would I make this appear in the same while loop?
  L
  L
  O 
  L
  L
  W
  L
  L
  Q
  </code></pre>
  <p>Sample output:</p>
  <pre><code>Toronto has played 7 games and has earned 10 points
  Montreal has played 8 games and has earned 3 points
  Toronto is in first place by 7 points
  </code></pre>
  <p><strong>UPDATE</strong>:</p>
  <p>My code:</p>
  <pre><code>else if (option == 2){
              int counter = 0;
              int totalpoints = 0;
              String phrase = keyboard.next();
              while(go){
                  String letter = keyboard.next();
                      if (letter.equals(&quot;W&quot;)){
                      pointsW++;
                      }
                      else if (letter.equals(&quot;L&quot;)){
                      pointsL++;
                      }
                      else if (letter.equals(&quot;O&quot;)){
                      pointsO++;
                      }
                      counter++;
                      if (letter.equals(&quot;N&quot;)){
                          totalpoints = pointsW + pointsL + pointsO;
                          counter--;
                          go = false;
                  }
              }
              int counter2 = 0;
              int totalpoints2 = 0;
              pointsW = 2;
              pointsL = 0;
              pointsO = 1;
              String phrase2 = keyboard.next();
                  while (go2){
                      String letter2 = keyboard.next();
                      if (letter2.equals(&quot;W&quot;)){
                      pointsW++;
                      }
                      else if (letter2.equals(&quot;L&quot;)){
                      pointsL++;
                      }
                      else if (letter2.equals(&quot;O&quot;)){
                      pointsO++;
                      }
                      counter2++;
                      if (letter2.equals(&quot;Q&quot;)){
                          counter2--;
                          totalpoints2 = pointsW + pointsL + pointsO;
                          go2 = false;
                      }
                  }
                              System.out.println(phrase + &quot; has played &quot;+counter+&quot; games and has earned &quot;+totalpoints+&quot; points&quot;);
                              System.out.println(phrase2 + &quot; has played &quot;+counter2+&quot; games and has earned &quot;+totalpoints2+&quot; points&quot;);
                      if (totalpoints &gt; totalpoints2){
                              System.out.println(phrase + &quot; is in first place by &quot;+(totalpoints - totalpoints2) + &quot; points&quot;);
                      }else{
                              System.out.println(phrase2 + &quot; is in first place by &quot;+(totalpoints2 - totalpoints) + &quot; points&quot;);
              }
          }
  </code></pre>
  <p>Sample input:</p>
  <pre><code>2
  Toronto
  W
  W
  L
  O
  W
  O
  W
  N
  Montreal 
  L
  L
  O 
  L
  L
  W
  L
  L
  Q
  </code></pre>
  <p><strong>The issue</strong>: This is the output I am getting &quot;Montreal played 8 games and has earned 11 points&quot; where instead it should be &quot;Montreal has played 8 games and has earned 3 points&quot;</p>
  <p><a href="https://i.stack.imgur.com/ejZj9.png" rel="nofollow noreferrer">The output I am getting</a></p>
  <hr />