title: Why do I get an error unless I ask for the same input twice? (Java)
body: <p>I run with program with only one <code>quit = input.nextLine();</code>, but this results in an error. However, if I use that line twice, no error occurs, and the program runs as intended. 
  <strong>Why?</strong></p>
  
  <p>Error Message: <code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
      at java.lang.String.charAt(String.java:686)
      at testjava.Test.main(Testjava.java:24)</code></p>
  
  <pre><code>    import java.util.Scanner;
  
      public class Test
      {
          public static void main(String[] args)
          {
              boolean x = true;
              boolean y = true;
              Scanner input = new Scanner(System.in);
  
              while (x) {
                  while (y) {
                      System.out.print("Enter -1 to quit ");
                      int val = input.nextInt();
                      if (val == -1)
                          y = false;
                  }  
  
                  System.out.print("Are you sure you want to quit? (y/n) ");
                  String quit = input.nextLine();
                  quit = input.nextLine();
  
                  if (quit.charAt(0) == 'y')
                      x = false;                  
              }
          }
  </code></pre>