title: while (scanner.hasNext()) loop not working twice? Java
body: <p>I have made a program which is like a vending machine! </p>
  
  <p>My code is similar to:  </p>
  
  <pre><code>public static void main (String [] args) {
      Scanner sc = new Scanner (System.in);
      while(sc.hasNext()) {
          String string = sc.next();
  
          sum = generateSum(sum)
          .....
      }
  }
  
  public static int generateSum(int sum) {
      Scanner sc = new Scanner (System.in);
      while (sc.hasNext()) {
          ....
      }
  
      return sum;
  }
  </code></pre>
  
  <p>Sorry for simplifying my code, but the normal one is very long! However, the problem is that I use <code>while (sc.hasNext())</code> loop twice. Basically I want to continue my main method until the input from the user is TERMINATE, but my program terminates after running once. </p>
  
  <p>I figured that if I take out my <code>generateSum</code> method, then the loop in my main method works fine so i guess it has to be something to do with have the <code>while (sc.hasNext())</code> loop twice. </p>
  
  <p>Any ideas how I can fix the problem?</p>