title: How to compare elements within an array and store specific elements into a separate array?
body: <p>I know similar questions have been asked. I've read through them and have developed what I thought was a reasonable solution to my problem. However, when I run the code it does not perform as I think it should. I'm obviously missing something here.</p>
  
  <p>I need to read in a string (a propositional logic statement) and determine how many variables it has. My thought process is: convert the string to a <code>charArray</code> and compare the elements. If a certain element is a letter AND is not equal to another element in the array, it is a new variable, thus adding to the variable count, and also needs to be stored in a separate array for later use. </p>
  
  <pre><code>public class PropLogic {
  public static void main(String[] args) {
      Scanner stdin = new Scanner(System.in);
  
      String statement;
      int numOfVariables = 0;
  
      System.out.println("Enter your propositional statement:");
      statement = stdin.nextLine();
      char[] charStatement = statement.toCharArray();
      char[] variables;
      variables = new char[25];
  
  // Counts number of variables in statement
     for (int i = 0; i &lt; statement.length(); i++){
         for(int j = i + 1; j &lt; statement.length(); j++){
             if(Character.isLetter(charStatement[i]) &amp;&amp; (charStatement[i] != 
  charStatement[j])){
                 variables[i] = charStatement[i];
                 numOfVariables++;
              }
          }
      }
      System.out.println("The number of variables is " + numOfVariables);
      System.out.println("The variables are " + new String(variables));
  }}
  </code></pre>
  
  <p>With input "hello" I get output of 9 variables and that the variables are "hell" when I want to be getting 4 variables that are "hello".   </p>