title: Counting the number of characters from a text file
body: <p>I currently have the following code:</p>
  
  <pre><code>public class Count {
  
      public static void countChar() throws FileNotFoundException {
          Scanner scannerFile = null;
  
          try {
              scannerFile = new Scanner(new File("file"));
          } catch (FileNotFoundException e) {
          }
  
          int starNumber = 0; // number of *'s
  
          while (scannerFile.hasNext()) {
              String character = scannerFile.next();
              int index =0;
              char star = '*';
              while(index&lt;character.length()) {
  
                  if(character.charAt(index)==star){
                      starNumber++;
                  }
                  index++;
              }
              System.out.println(starNumber);
          }
      }
  </code></pre>
  
  <p>I'm trying to find out how many times a * occurs in a textfile. For example given a text file containing
   Hi * My * name *</p>
  
  <p>the method should return with 3</p>
  
  <p>Currently what happens is with the above example the method would return:</p>
  
  <p>0
  1
  1
  2
  2
  3</p>
  
  <p>Thanks in advance.</p>