The result correctly gives the words, but not the characters or lines. How can I make the Scanner go back to the beginning of the file? Using three Scanners did not work.
public static void main (String[] args) throws FileNotFoundException
{
int iCharCount = 0;
int iWordCount = 0;
int iLineCount = 0;
Scanner scConsole = new Scanner(System.in);
System.out.println("Input File:");
String sInputFile = scConsole.next();
File inputFile = new File(sInputFile);
Scanner in = new Scanner(inputFile);
//gets the number of words
while(in.hasNext())
{
String sInput = in.next();
iWordCount++;
}
System.out.println("Words: " + iWordCount);
//gets the number of characters
while(in.hasNext())
{
char ch = in.next().charAt(0);
iCharCount++;
}
System.out.println("Characters: " + iCharCount);
//gets the number of lines
while(in.hasNextLine())
{
String sLine = in.nextLine();
iLineCount++;
}
System.out.println("Lines: " + iLineCount);
scConsole.close();
in.close();
}
So, when I input a text file containing the Sentence: "The cat is in the hat." The result is: Words: 6 Characters: 0 Lines: 0