title: How do I count the number of characters in a file using scanner and output it?
body: <p>I am currently working on a program and cannot figure out why whenever I print the length it does not print out the correct number. Both uppercase and lowercase work correctly. </p>
  
  <p>An example Input would be:</p>
  
  <blockquote>
    <p>I</p>
    
    <p>Love</p>
    
    <p>Programming</p>
  </blockquote>
  
  <p>The output would then be:</p>
  
  <blockquote>
    <p>Number of characters is 16.</p>
    
    <p>Number of lower case letters is 13.</p>
    
    <p>Number of upper case letters is 3.</p>
  </blockquote>
  
  <pre><code>public static void main(String[] args) throws IOException 
      {
      int lowercase = 0;
      int uppercase = 0;
      int length = 0;
      File file1 = new File("input.txt");
      Scanner scanner = new Scanner(file1);
      while(scanner.hasNext())
      {
      String s = scanner.nextLine();
      length = s.length();
      char[] charAnalysis = s.toCharArray();
      for (char element : charAnalysis) 
      {
          if (Character.isUpperCase(element)) 
          {
              uppercase++;
          }
          else if (Character.isLowerCase(element)) 
          {
              lowercase++;
          }
          length = s.length();
      }
      }
      File file2 = new File("output.txt");
          try (PrintStream ps = new PrintStream(file2)) 
          {
              ps.println("Number of characters is " + length);
              ps.println("Number of lower case letters is " + lowercase);
              ps.println("Number of upper case letters is " + uppercase);   
          }
          catch(IOException e) 
          { 
  
          }
      }
  }
  </code></pre>