here are the instructions for my assignment
Write a program that counts the number of words, lines and total characters (not including whitespace) in a paper, assuming that consecutive words are separated either by white space or end-of-line characters.
Your output from this particular file should say:
line count: 58 word count: 1673 character count: 7490(7492)

this is my code

import java.util.*;
import java.io.*;

public class Homework6 {
    public static void main(String[] args) 
		throws FileNotFoundException {
		Scanner input = new Scanner (new File("IHaveADream.txt"));
	
		int linecount;
		int wordcount;
		int charactercount;
		
		linecount = 0;
			while (input.hasNextLine()) {
			linecount++;
			}
		wordcount = 0;
			while (input.hasNext()) {
			wordcount++;
			}			
		charactercount = 0;
			while (input.hasNext()) {
			charactercount++;
			}						
		System.out.print( "line count: " + linecount + "	word count: "+ wordcount + "	character count:" + charactercount);
	}

	}

ok so when i compile there are no errors but it does not run any help please!!!

ok i did some cleaning up

import java.util.*;
import java.io.*;

public class Homework6 {
    public static void main(String[] args) 
		throws FileNotFoundException {
		Scanner input = new Scanner (new File("IHaveADream.txt"));
			
		int linecount = 0;
			while (input.hasNextLine()) {
			String line = input.nextLine();
			linecount++;
			}
			
		int wordcount = 0;
			while (input.hasNext()) {
			String word = input.next();
			wordcount++;
			}			

		int charactercount = 0;
			while (input.hasNext()) {
			String word = input.next();
			charactercount++;
			}				
		System.out.println( "line count: " + linecount +"	word count: "+ wordcount + "	character count:" + charactercount);
	}

	}

it prints out the line correctly but after that wordcount and charactercount are 0
i've relized it is because it stopped at the end of the document after wordcount but i do not know how to get it to parse the document again help please

Member Avatar for james.newell

Hi pirateninja1111.

You are correct. You have no errors in your program. However your program never ends once it is started because the condition input.hasNext*() always evaluates to true because inside the while loops you never actually get the next input so the scanner keeps saying yes there is more input.

Inside your loops you need a input.next() statement to read the next input from the scanner. This will cause the line count to work correctly. However the wordcount and character count won't work because all the input will be consumed.

The simplest but least efficient way of fixing the word count is to re-initialise the scanner

input = new Scanner (new File("IHaveADream.txt"));

before the while loops allowing the scanner to read from the beginning of the file again.

Also I think the scanner default behaviour is to read strings separated by whitespace and newlines so that the scanner won't read individual characters and your charactercount loop will return the same output as the word count.

A more efficient way of achieving your outcomes would be to read the file a single character at a time (check out the FileReader class) and keep count of each newline, whitespace and other character consumed.

Hope that helps.

thank you so much that helped out a lot :) i'll try that i'm just learning so i'm trying to get it to work first :) and it does now although my charactercount is coming up wrong, it is coming up the same as the wordcount i can't find the reason why any answers?

Member Avatar for james.newell

Check out the comments in my last post

Also I think the scanner default behaviour is to read strings separated by whitespace and newlines so that the scanner won't read individual characters and your charactercount loop will return the same output as the word count.

A more efficient way of achieving your outcomes would be to read the file a single character at a time (check out the FileReader class) and keep count of each newline, whitespace and other character consumed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.