title: Java Question on how to cancel system input
body: <p>I want to print last message when user inputs q but it gives an error.</p>
  <hr />
  <h2>Language Translator Program</h2>
  <p>Please enter the input sentence (press q to exit):</p>
  <p>Robin came to Montreal, Canada in 2009.</p>
  <p>Robin stays in Montreal for 11 years. Montreal is in Canada.</p>
  <p>Please enter the input sentence (press q to exit):</p>
  <p>Lucy came to Berlin, Germany in 2000.</p>
  <p>Lucy stay in Berlin for 20 years. Berlin is in Germany.</p>
  <p>Please enter the numbers along operation (press q to exit):</p>
  <p>q</p>
  <p>Thanks for using translator program.</p>
  <pre><code>import java.util.Scanner;
  
  public class MiniTranslator {
      public static void main(String[] args) {
          String message =&quot;&quot;;
          do {
              //Declaring scanner
              Scanner keyboard = new Scanner(System.in);
  
              //Printing greeting message
  
              System.out.println(&quot;---------------------------------------------------------\n&quot; 
                                   +&quot;\tLanguage Translator Program\n&quot;
                                  +&quot;---------------------------------------------------------&quot;);
  
              System.out.println(&quot;Please enter the input sentence (press q to exit): \n&quot;);
              message = keyboard.nextLine();
              String[] arr = message.split(&quot;[ ,.]+&quot;,8);
              String name = arr[0];
              String city = arr[3];
              String country = arr[4];
              String year = arr[6];
  
              int noOfYears =2020 - (Integer.parseInt(year));
  
              System.out.println(&quot;\n&quot; + name +&quot; stay in &quot; + city +&quot; for &quot; + noOfYears +&quot; years. &quot; 
                                  + city +&quot; is in &quot; + country +&quot;.&quot;);
              System.exit('q');
              keyboard.close();
          }
          while(message!=&quot;q&quot;);
          if(message ==&quot;q&quot;) {
              System.out.println(&quot;Thanks for using translator program&quot;);
          }
      }
  }
  </code></pre>