title: Can't use Scanner.nextInt() and Scanner.nextLine() together
body: <p>I have to get a string input and an integer input, but there order of input should be that integer comes first then user should be asked for string input</p>
  
  <pre><code>Scanner in = new Scanner(System.in);
  
  
      input = in.nextLine();
      k = in.nextInt();
  
      in.close();
  </code></pre>
  
  <p>The above code works fine but if I take an integer input first like in the following code</p>
  
  <pre><code>Scanner in = new Scanner(System.in);
  
      k = in.nextInt();
      input = in.nextLine();
  
  
      in.close();
  </code></pre>
  
  <p>then it throws the java.lang.ArrayIndexOutOfBoundsException.</p>
  
  <p>Here's the complete code of my source file:</p>
  
  <pre><code>import java.util.Scanner;
  </code></pre>
  
  <p>public class StringSwap {</p>
  
  <pre><code>public static void main(String args[]) {
      String input;
      int k;
  
      Scanner in = new Scanner(System.in);
  
      k = in.nextInt();
      input = in.nextLine();
  
  
      in.close();
  
      int noOfCh = noOfSwapCharacters(input);
      originalString(input, noOfCh, k);
  
  }
  
  public static int noOfSwapCharacters(String s) {
  
      char cS[] = s.toCharArray();
      int i = 0, postCounter = 0;
      while (cS[i] != '\0') {
          if (cS[i] != '\0' &amp;&amp; cS[i + 1] != '\0') {
  
              cS[cS.length - 1 - postCounter] = '\0';
  
              postCounter++;
  
          }
          i++;
      }
  
      return postCounter;
  
  }
  
  public static void originalString(String s, int noOfCh, int k) {    
      int counter = 1, chCounter = 0;
      char cArray[] = s.toCharArray();
      String post = "";
      String pre = "";
      String finalString = "";
      char temp;
  
      for (int i = 1; i &lt;= k; i++) {
          chCounter = 0;
          counter = 1;
          post = "";
          pre = "";
  
          for (int j = 0; j &lt; cArray.length; j++) {
  
              if (counter % 2 == 0 &amp;&amp; chCounter &lt;= noOfCh) {
                  temp = cArray[j];
                  post = temp + post;
                  cArray[j] = '\0';
                  chCounter++;
  
              }
              counter++;
  
          }
          for (int h = 0; h &lt; cArray.length; h++) {
  
              if (cArray[h] != '\0')
                  pre = pre + cArray[h];
  
          }
  
          finalString = pre + post;
          for (int l = 0; l &lt; finalString.length(); l++) {
              cArray[l] = finalString.charAt(l);
  
          }
  
      }
  
      System.out.println(finalString);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Kindly point out what I am doing wrong here.</p>