Error in counting number of words and characters from a file in java
<p>I have written the following code to print file content and print the number of character and words from file</p>

<pre><code>import java.io.*;
import java.util.*;

 class Ass53
{ 


public static void main(String args[]) throws Exception
{
    File file=new File("sample.txt");
    Scanner sc=new Scanner(new FileInputStream(file));
    String line,line1;
    int count1=0;
    int count=0;        

    /*Loop for printing contents of file*/
    while(sc.hasNextLine())
    {
        line=sc.nextLine();
        System.out.println(line);
    }


    /*loop for counting number of character in file*/
    while(sc.hasNext())
    {
            line1=sc.next();
        for(int i=1;i&lt;=line1.length();i++)
            count1++;
    }
    System.out.println(count1); 


    /*loop for counting number of words in a file*/
    while(sc.hasNext())
    {
            sc.next();
            count++;
    }
    System.out.println("Number of words: " + count);


    }


 }
</code></pre>

<p>The problem is that only first while loop is executing.I guess the reason may be the sc.nextLine for first while loop.After first while loop sc points to nothing i guess?.</p>

<p>Is there any way to fix it?
I want that my other while loop also work</p>
