title: Java, counting letters in file
body: <p>Im trying to read the number of letters from the file that the user types in, in this case: "cyphertext.txt". The problem I'm having is that instead of outputting that the file has 94 "E"s or 27 "Z"s in it, it is outputting the file name's number of letters, so "ciphertext.txt" has 2 "T"s. (This was coded in BlueJ)</p>
  
  <p>Here is the code from <em>Frequency Tester</em>:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.File;
  import java.util.Scanner;
  public class FrequencyTester
  {
      public static void main(String[] args) throws IOException
      {
  
          //freq.findFile();
  
  
          Scanner in = new Scanner(System.in);
          String fileNameStr = "";
          while(! (new File (fileNameStr + ".txt").isFile()))
          {
              System.out.print("Please input the name of your desired file (no file extension required): "); 
              fileNameStr = in.next() + in.nextLine(); 
              if(new File (fileNameStr + ".txt").isFile())
              {
                  System.out.println("\nFile accepted."); 
                  Frequency freq = new Frequency(fileNameStr);
                  Scanner inFile = new Scanner(fileNameStr + ".txt");
                  freq.fileWords(inFile);
                  freq.frequencyPrinter();
  
              }
              else
              {
                  System.out.println("\nError: Invalid File Name");
              }
          }
      }
  
  }
  </code></pre>
  
  <p>Here is the code from <em>Frequency</em>:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.File;
  import java.util.Scanner;
  public class Frequency
  {
      // instance variables - replace the example below with your own
      private int x;
      private String fileNameStr, oneWord, oneChar; 
      public final char [] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
      public int [] alphaCounter = new int[alphabet.length];
  
      /**
       * Constructor for objects of class Frequency
       */
      public Frequency(String fileNameStr) throws IOException
      {
          // initialise instance variables
          //File fileName = new File (fileNameStr + ".txt");
          PrintWriter outFile = new PrintWriter(new File(fileNameStr +"Frq.txt"));
          //Scanner inFile = new Scanner(fileName);
  
          for(int h = 0; h &lt; alphaCounter.length; h++)
              alphaCounter[h] = 0; 
      }
  
      public void fileWords(Scanner inFile) 
      {
          while(inFile.hasNext())
          {
              oneWord = inFile.next(); 
              System.out.println(oneWord); 
              for(int i = 0; i &lt; oneWord.length(); i++)
              {
                  if(!Character.isWhitespace(oneWord.charAt(i)))
                      charCounter(i);
              }
          }
      }
      public void charCounter(int i)
      {
          for(int j = 0; j &lt; alphabet.length; j++)
          {
              if(oneWord.charAt(i) == alphabet[j])
              {
                  alphaCounter[j]++; 
              }
          }
      }
      public void frequencyPrinter()
      {
          System.out.print("\n");
          for(int k = 0; k &lt; alphabet.length; k++)
          {
              System.out.print(alphabet[k] + " - " + alphaCounter[k] + "\n");
          }
      }
  }
  </code></pre>
  
  <p>Here is the setup in BlueJ and the Output:</p>
  
  <p><a href="https://i.stack.imgur.com/8EkP8.png" rel="nofollow noreferrer">BlueJ</a></p>
  
  <p><a href="https://i.stack.imgur.com/atHSY.png" rel="nofollow noreferrer">Output</a></p>