title: Word frequency and line number in Java
body: <blockquote>
    <p>Write a program using Java or C# that counts the frequencies of each
    word in a text, and output each word with its count and line numbers
    where it appears. We define a word as a contiguous sequence of
    non-white-space characters. (hint: split()) Note: different
    capitalizations of the same character sequence should be considered
    same word, e.g. Python and python, I and i. The input will be several
    lines with the empty line terminating the text (using text file for
    input is optional). Only alphabet characters and white spaces will be
    present in the input. The output is formatted as follows: </p>
  
  <pre><code>- 1 python 1
  - 3 is 1 2
  - 1 a 1
  - 1 but 1
  - 1 cool 1 2
  - 1 even 2
  - 1 object 2
  - 1 oriented 2
  - 1 it 2
  - 1 language 1 2
  - 1 Java 1
  - 1 purely 2
  - 1 since 2
  </code></pre>
  </blockquote>
  
  <p>This is what I have:</p>
  
  <pre><code>public class Test {
      public static void main(String[] args) 
      {
          String text = "Python is a cool language but Java \n" +
          "is also cool since it is purely object oriented language ";
          String[] keys = text.split(" ");
          String[] uniqueKeys;
          int count = 0;
          System.out.println(text);
          uniqueKeys = getUniqueKeys(keys);
          int line2 = text.indexOf('\n');
  
          for(String key: uniqueKeys)
          {
              if(null == key)
              {
                  break;
              }           
              for(String s : keys)
              {
                  if(key.equals(s))
                  {
                      count++;
                  }               
              }
  
              System.out.println(count +" "+ key);
              count=0;
          }
      }
  
      private static String[] getUniqueKeys(String[] keys)
      {
          String[] uniqueKeys = new String[keys.length];
  
          uniqueKeys[0] = keys[0];
          int uniqueKeyIndex = 1;
          boolean keyAlreadyExists = false;
  
          for(int i=1; i&lt;keys.length ; i++)
          {
              for(int j=0; j&lt;=uniqueKeyIndex; j++)
              {
                  if(keys[i].equals(uniqueKeys[j]))
                  {
                      keyAlreadyExists = true;
                  }
              }           
  
              if(!keyAlreadyExists)
              {
                  uniqueKeys[uniqueKeyIndex] = keys[i];
                  uniqueKeyIndex++;               
              }
              keyAlreadyExists = false;
          }       
          return uniqueKeys;
      }
  }
  </code></pre>
  
  <p>I can't figure out how to get the output to include the line numbers of each word as well. Thanks for any help. I am using Java by the way.</p>