title: Input Mismatch Exception on a method calling Scanner's nextLine
body: <p>I need to use the method </p>
  
  <pre><code>    public static String readString() { 
  
      return input.nextLine(); 
  }
  </code></pre>
  
  <p>In order to use the Scanner method next.Line(), its just a requirement of the project, the thing is that when I use the static method ProjectUtils.readString() to gather the user input as a string it just throws me a lot of errors, I cant understand why, Im using it this way </p>
  
  <pre><code>       public static void encryptAString() {
  
          ProjectUtils.println("Enter the phrase and the key");
          String p = ProjectUtils.readString();
          ProjectUtils.println("Enter key");
          int k = ProjectUtils.readInteger();
          ProjectUtils.println(EncryptString.encryptString(p,k ));
  </code></pre>
  
  <p>But always ends up terminating and throwing this </p>
  
  <pre><code>Exception in thread "main" java.util.InputMismatchException
  at java.util.Scanner.throwFor(Unknown Source)
  at java.util.Scanner.next(Unknown Source)
  at java.util.Scanner.nextInt(Unknown Source)
  at java.util.Scanner.nextInt(Unknown Source)
  at ProjectUtils.readInteger(ProjectUtils.java:24)
  at ProjectUtils.encryptAString(ProjectUtils.java:88)
  at StringMenuManager.run(StringMenuManager.java:37)
  at ProjectUtils.operationsOnStrings(ProjectUtils.java:69)
  at MainMenuManager.run(MainMenuManager.java:47)
  at P1Main.main(P1Main.java:9)
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>MCVE</p>
  
  <p>The main class</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          MCVE.encryptAString();
      }
  }
  </code></pre>
  
  <p>The MCVE method or ProjectUtils class in the original project</p>
  
  <pre><code>import java.util.Scanner;
  
  
  
  
  
  public class MCVE {
  
  private static final Scanner input = new Scanner(System.in);
  
  public static void println(String s) { 
      System.out.println(s); 
  }
  public static int readInteger() { 
      // for the moment, just assume that the input is a
      // valid integer.... but eventually we want to be 
      // more robust and explicitly read and test first if
      // the input was really an integer or not....
      return input.nextInt(); 
  }
  public static String readString() { 
      // for the moment, just assume that the input is a
      // valid String.... but eventually we want to be 
      // more robust and explicitly read and test first if
      // the input was really an integer or not....
      return input.nextLine(); 
  }
  
  public static void encryptAString() {
  
      MCVE.println("Enter the phrase and the key");
      String p = MCVE.readString();
      int k = MCVE.readInteger();
      MCVE.println(EncryptString.encryptString(p,k ));
  
       }
     }
  </code></pre>
  
  <p>And the encryption class</p>
  
  <pre><code> public class EncryptString {
   private static final int ALENGTH = 26; 
  
  /**
   * Encrypt a string using a given key value. 
   * @param s the String to be encrypted
   * @param key the key 
   * @return the encrypted string
   */
  public static String encryptString(String s, int key) {
      // if the key value is not in the accepted range (-25..25)
      // the encrypted string is the same as the input string
      if (key &lt; -25 || key &gt; 25) 
          return s; 
  
      // the key is valid, construct the encrypted string as 
      // described in P1 specs...
      String newString = "";
      for(int x = 0; x&lt;s.length(); x++)
      {
          if(((int)s.charAt(x)&gt;64&amp;&amp;(int)s.charAt(x)&lt;123))
              newString = newString + encryptChar(s.charAt(x), key);
          else
              newString = newString + s.charAt(x);
  
      }
      return newString;
  }
  
  
  /**
   * Encrypt a particular character.
   * @param ch the character to encrypt - assumed to be a letter ‘a’..’z’ or ‘A’..’Z’
   * @param key the key to be used. It is assumed to be a value in range (-25..25)
   * @return the new character after encryption
   */
  private static char encryptChar(char ch, int key) {
      // PRE: ch is a letter 'A'..'Z' or 'a'..'z'
      // PRE: key is an integer in the range -25..25
  
      int base; 
      if (Character.isUpperCase(ch))
          base = (int) 'A'; 
      else 
          base = (int) 'a'; 
      return (char) (Math.abs((((int) ch - base) + key + ALENGTH) 
                                  % ALENGTH) + base); 
  }
  </code></pre>
  
  <p>}</p>