title: How to return last letter in ArrayList and return total number of letters entered?
body: <p>I have been trying to do a question as follows:</p>
  <blockquote>
  <p>The program should keep prompting the user to enter a letter until the user types ‘!’, which ends the program. Each time the use enters a letter, the program should add the letter to the previous letters entered and print the result. The program should also return:</p>
  <ul>
  <li>the last entered letter when ‘@’ is entered.</li>
  <li>the total number of letters entered when ‘#’ is entered.</li>
  </ul>
  </blockquote>
  <p>I am stuck on how to return the last entered letter and returning number of letters entered. I thought about if statements but they don't seem to be working. Here is my code so far:</p>
  <pre><code>import java.util.Scanner;
  import java.util.ArrayList;
  public class check {
  
  static Scanner input = new Scanner (System.in);
  static ArrayList&lt;String&gt; array= new ArrayList&lt;&gt;();
  
  public static void main(String[] args) {
      System.out.print(&quot;Please enter a letter: &quot;);
      String a=input.nextLine();
      while ( !a.equals(&quot;!&quot;)) {
          array.add(a);
          for(String b : array) {
              System.out.print(b);
          }
          System.out.print(&quot;\n&quot;+&quot;Please enter a letter: &quot;);
          a=input.nextLine();
      }
      // array.add(a);
      if((a.equals(&quot;#&quot;))) {
          ArrayList.get(ArrayList.size()-1);
          for(String b : array) {
              System.out.print(b);
          }
      }
  </code></pre>
  <p>I know the if statement I have done is incorrect because it's giving red line under. But don't know how and what to do next.</p>