0

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

rsk997
  • 1
  • 1
    I don't think `Scanner` fits your needs here, a simple loop should be enough (`for(int i=0; i – NiziL Nov 12 '14 at 14:52
  • Get each line. Count the number of words and the number of character in each word – Spikatrix Nov 12 '14 at 14:56
  • 2
    Instead of doing it in three loop do it in one. You read a file till end with `while(in.hasNext())` the next loop will not be treat. – SleepyX667 Nov 12 '14 at 14:56

4 Answers4

1

better way is to read the whole file as String and then you could write a class like:

public class WordCount {
    private static String SEPARATOR = System.getProperty("line.separator");
    private String text;

    public WordCount(final String text) {
        this.text = text;
    }

    public int chars() {
        return text.length();
    }

    public int words() {
        if (text.isEmpty()) {
           return 0;
        }
        return text.trim().split("\\s+").length;
    }

    public int lines() {
        if (text.isEmpty()) {
          return 0;
        }
        return text.trim().split(SEPARATOR).length;
    }

    public void setText(final String text) {
        this.text = text;
    }
}

Your main could be like:

public class Main {
    public static void main(String[] args) {
        final String text = readTheWholeText(); // you have to implement this
        WordCount wc = new WordCount(text);
        final int chars = wc.chars();
        final int lines = wc.lines();
        final int words = wc.words();
        System.out.println("words = " + words + "  chars = " + chars + "  lines = " + lines);
    }
}

btw read a whole text file look at: Reading from a text file and storing in a String or Java: How to read a text file

Community
  • 1
  • 1
SleepyX667
  • 670
  • 9
  • 21
  • 1
    That `trim()` is not really needed in the method `lines()`. But still a good answer. – Tom Nov 12 '14 at 15:50
0

This is because Scanner maintains a cursor for reading a file line by line. So each time, you use in.next, the cursor is re positioned to match the new line. because of that, even in the example that you have provided, in.next reads the first line. when in.next is used again, it moves the cursor to the next line. But here there is only one line. Thats y you are getting the line count and char count as 0.

A better way will to store that in.next in a String variable, and then use it to find the number of characters.

And accordingly make changes in the line number

FatherMathew
  • 960
  • 12
  • 15
0

I assume this is homework. I don't think Scanner is the best option here, probably BufferedReader would be better, but Scanner will work. You don't need to backup, you can do this easily one time through the file.

You are doing it backwards, don't count words first, grab each line (and keep a count of those), then split the line on whitespace to get your word count, then count the characters in each word.

Something like (this is untested, just putting you on the right track):

 //gets the number of lines
int iLineCount = 0;
    int wordCount = 0;
    int characterCount = 0;
    while (in.hasNextLine()) {
        String sLine = in.nextLine();
        iLineCount++;
        String[] words = sLine.split(" ");
        wordCount =+ words.length;
        for (String word : words) {
            characterCount =+ word.length();
        }
    }
Michael
  • 2,683
  • 28
  • 30
  • BTW, this counts punctuation as part of the character count. Adjust accordingly if that isn't right. – Michael Nov 12 '14 at 15:10
0

Assuming it is a homework it is better to try on your own, but you can try the following for each case (if allowed to use them):

1) For line count: split text by line separators, then get the array size.

2) For word count: split text by space delimiters and line separators, then get the array size.

3) For character count: use String length() method.

Search for String split(...) and length() methods.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54