count characters, words and lines in file - java

This should count number of lines, words and characters into file.
But it doesn't work. From output it shows only 0.
Code:
public static void main(String[] args) throws IOException {
int ch;
boolean prev = true;
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner in = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
}
// count the characters of the file till the end
while(in.hasNext()) {
ch = in.next().charAt(0);
if (ch != ' ') ++charsCount;
if (!prev && ch == ' ') ++wordsCount;
// don't count if previous char is space
if (ch == ' ')
prev = true;
else
prev = false;
if (ch == '\n') ++linesCount;
}
//display the count of characters, words, and lines
charsCount -= linesCount * 2;
wordsCount += linesCount;
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
in.close();
}
I can't understand what's going on.
Any suggestions?

Different approach. Using strings to find line,word and character counts:
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner in = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
}
while (in.hasNext()) {
String tmpStr = in.nextLine();
if (!tmpStr.equalsIgnoreCase("")) {
String replaceAll = tmpStr.replaceAll("\\s+", "");
charsCount += replaceAll.length();
wordsCount += tmpStr.split(" ").length;
}
++linesCount;
}
//display the count of characters, words, and lines
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
in.close();
}
Note:
For other encoding styles use new Scanner(new File(selectedFile), "###"); in place of new Scanner(selectedFile);.
### is the Character set to needed. Refer this and wiki

Your code is looking at only the first characters of default tokens (words) in the file.
When you do this ch = in.next().charAt(0), it gets you the first character of a token (word), and the scanner moves forward to the next token (skipping rest of that token).

You have a couple of issues in here.
First is the test for the end of line is going to cause problems since it usually isn't a single character denoting end of line. Read http://en.wikipedia.org/wiki/End-of-line for more detail on this issue.
The whitespace character between words can be more than just the ASCII 32 (space) value. Consider tabs as one case. You want to check for Character.isWhitespace() more than likely.
You could also solve the end of line issues with two scanners found in How to check the end of line using Scanner?
Here is a quick hack on the code you provided along with input and output.
import java.io.*;
import java.util.Scanner;
import javax.swing.JFileChooser;
public final class TextApp {
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner fileScanner = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
fileScanner = new Scanner(selectedFile);
}
while (fileScanner.hasNextLine()) {
linesCount++;
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
// count the characters of the file till the end
while(lineScanner.hasNext()) {
wordsCount++;
String word = lineScanner.next();
charsCount += word.length();
}
lineScanner.close();
}
//display the count of characters, words, and lines
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
fileScanner.close();
}
}
Here is the test file input:
$ cat ../test.txt
test text goes here
and here
Here is the output:
$ javac TextApp.java
$ java TextApp
# of chars: 23
# of words: 6
# of lines: 2
$ wc test.txt
2 6 29 test.txt
The difference between character count is due to not counting whitespace characters which appears to be what you were trying to do in the original code.
I hope that helps out.

You could store every line in a List<String> and then linesCount = list.size().
Calculating charsCount:
for(final String line : lines)
charsCount += line.length();
Calculating wordsCount:
for(final String line : lines)
wordsCount += line.split(" +").length;
It would probably be a wise idea to combine these calculations together as opposed to doing them seperately.

Use Scanner methods:
int lines = 0;
int words = 0;
int chars = 0;
while(in.hasNextLine()) {
lines++;
Scanner lineScanner = new Scanner(in.nextLine());
lineScanner.useDelimiter(" ");
while(lineScanner.hasNext()) {
words++;
chars += lineScanner.next().length();
}
}

Looks like everyone is suggesting you an alternative,
The flaw with your logic is, you are not looping through the all the characters for the entire line. You are just looping through the first character of every line.
ch = in.next().charAt(0);
Also, what does 2 in charsCount -= linesCount * 2; represent?
You might also want to include a try-catch block, while accessing a file.
try {
in = new Scanner(selectedFile);
} catch (FileNotFoundException e) {}

Maybe my code will help you...everything work correct
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class LineWordChar {
public static void main(String[] args) throws IOException {
// Convert our text file to string
String text = new Scanner( new File("way to your file"), "UTF-8" ).useDelimiter("\\A").next();
BufferedReader bf=new BufferedReader(new FileReader("way to your file"));
String lines="";
int linesi=0;
int words=0;
int chars=0;
String s="";
// while next lines are present in file int linesi will add 1
while ((lines=bf.readLine())!=null){
linesi++;}
// Tokenizer separate our big string "Text" to little string and count them
StringTokenizer st=new StringTokenizer(text);
while (st.hasMoreTokens()){
`enter code here` s = st.nextToken();
words++;
// We take every word during separation and count number of char in this words
for (int i = 0; i < s.length(); i++) {
chars++;}
}
System.out.println("Number of lines: "+linesi);
System.out.println("Number of words: "+words);
System.out.print("Number of chars: "+chars);
}
}

public class WordCount {
/**
* #return HashMap a map containing the Character count, Word count and
* Sentence count
* #throws FileNotFoundException
*
*/
public static void main() throws FileNotFoundException {
lineNumber=2; // as u want
File f = null;
ArrayList<Integer> list=new ArrayList<Integer>();
f = new File("file.txt");
Scanner sc = new Scanner(f);
int totalLines=0;
int totalWords=0;
int totalChars=0;
int totalSentences=0;
while(sc.hasNextLine())
{
totalLines++;
if(totalLines==lineNumber){
String line = sc.nextLine();
totalChars += line.length();
totalWords += new StringTokenizer(line, " ,").countTokens(); //line.split("\\s").length;
totalSentences += line.split("\\.").length;
break;
}
sc.nextLine();
}
list.add(totalChars);
list.add(totalWords);
list.add(totalSentences);
System.out.println(lineNumber+";"+totalWords+";"+totalChars+";"+totalSentences);
}
}

Related

Read from File ASCII art Replace, Write to File

Trying to read text from ASCII art text file (it contains a series of blank spaces and '*' characters). Then, I want to be able to change the asterisks to whatever character the user wishes.
I also want to select a random file. All three files exist. I can store them as a list, if needed.
When executed, this code prompts user for substitution text but then just returns the file read location (including the random number 1-3).
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class AsciiArt
{
public static void main(String[]args) throws IOException
{
System.out.println("ASCII Art\n");
Scanner kbd = new Scanner(System.in);
Random randomGen = new Random();
int verSub = randomGen.nextInt(3)+1;
String fullSub = "/user/****/data/****"+verSub+".dat";
File inputFile = new File(fullSub);
System.out.print("Enter substitution text? ");
String subText = kbd.next();
Scanner input = new Scanner(fullSub);
PrintWriter outputFile = new PrintWriter("newFile.txt")
String line;
while(input.hasNext()){
line = input.nextLine();
int i, subIdx = 0;
for (i = 0; i < line.length(); i++){
if(line.charAt(i) == '*'){
outputFile.print(subText.charAt(subIdx));
++subIdx;
}
if(subIdx == subText.length()){
subIdx = 0;
}
else{
outputFile.print(line.charAt(i));
}
}
}
input.close();
outputFile.close();
System.out.println();
}
}

Asking for a word and checking its number of occurrence in a text file

public class Try{
public static void main (String args[]) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("Try.txt"));
Scanner sc = new Scanner(System.in);
System.out.print("Enter the subtring to look for: ");
String Word=sc.next();
String line=in.readLine();
int count =0;
String s[];
do
{
s=line.split(" ");
for(int i=0; i < s.length; i++)
{
String a = s[i];
if(a.contains(Word))
count++;
}
line=in.readLine();
}while(line!=null);
System.out.print("There are " +count+ " occurences of " +Word+ " in ");
java.io.File file = new java.io.File("Try.txt");
Scanner input = new Scanner(file);
while(input.hasNext())
{
String word = input.nextLine();
System.out.print(word);
}
}
}
The intended purpose of my program is to ask the user for a certain word(s) that will be checked in a text file and if it exists, it will count the number of times the user-entered word occurs in the text file. So far, my program can only search for one word. If I try two words separated by space, only the first word will be searched and counted for its number of occurrence. Any tips on how to search multiple words?
I was following literally the title of the question and therefore I will suggest this algorithm:
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
Scanner sc = new Scanner(System.in);
System.out.print("Enter the subtring to look for: ");
String word = sc.next();
String line = in.readLine();
int count = 0;
// here is where the efficiently magic happens
do {
// 1. you dont need to split a line by spaces, too much overhead...
// 2. and you dont need to do counter++
// 3. do instead: calculate the number of coincidences that the word is
//repeated in a whole line...that is what the line below does..
count += (line.length() - line.replace(word, "").length()) / word.length();
//the rest looks fine
//NOTE: if you need a whole word then wrap the input of the user and add the empty spaces at begin and at the end...so the match will be perfect to a word
line = in.readLine();
} while (line != null);
System.out.print("There are " + count + " occurences of " + word + " in ");
}
Edit:
if you want to check more than one word in the document then use this
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
Scanner sc = new Scanner(System.in);
System.out.print("Enter the subtring to look for: ");
String input = sc.nextLine();
String[] word = input.split(" ");
String line = in.readLine();
int count = 0;
do {
for (String string : word) {
count += (line.length() - line.replace(string, "").length()) / string.length();
}
line = in.readLine();
} while (line != null);
System.out.print("There are " + count + " occurences of " + Arrays.toString(word) + " in ");
}

How to Extract Multiple words from a string using IndexOf and substring Java?

I have a file i imported through system, Now i am stuck. Using while loops and if statements, and WITHOUT the help of the Split() method, How could i first, Read the file, line by line with the scanner? Then second how could i pull the words out one by one, As i pull out one word, A variable, countWords has to increase by one, say there is 5 words in a string, I would need to run through the loop 5 times and countWords would become 5.
This is the code i have so far, Kind of crappy.
import java.util.Scanner;
import java.io.*;
class Assignmentfive
{
private static final String String = null;
public static void main(String[] args) throws FileNotFoundException
{
Scanner scan = new Scanner(new File("asgn5data.txt"));
int educationLevel = 0;
String fileRead = "";
int wordCount = 0;
while (scan.hasNext() && !fileRead.contains("."))
{
fileRead = scan.nextLine();
int index = fileRead.indexOf(" ");
String strA = fileRead.substring(index);
System.out.print(strA);
wordCount++;
}
There is more to my code, however it is just a few calculations commented out.
Thanks!
Here is how I would refactor your while loop to correctly extract, print, and count all words in a sentence:
while (scan.hasNext()) {
int wordCount = 0;
int numChars = 0;
fileRead = scan.nextLine();
// Note: I add an extra space at the end of the input sentence
// so that the while loop will pick up on the last word.
if (fileRead.charAt(fileRead.length() - 1) == '.') {
fileRead = fileRead.substring(0, fileRead.length() - 1) + " ";
}
else {
fileRead = fileRead + " ";
}
int index = fileRead.indexOf(" ");
do {
String strA = fileRead.substring(0, index);
System.out.print(strA + " ");
fileRead = fileRead.substring(index+1, fileRead.length());
index = fileRead.indexOf(" ");
wordCount++;
numChars += strA.length();
} while (index != -1);
// here is your computation.
if (wordCount > 0) {
double result = (double)numChars / wordCount; // average length of words
result = Math.pow(result, 2.0); // square the average
result = wordCount * result; // multiply by number of words
System.out.println(result); // output this number
}
}
I tested this code by hard-coding the string fileRead to be your first sentence The cat is black.. I got the following output.
Output:
The
cat
is
black

Using four methods to count and display the lines, words, and characters in a file in java

This is my first time using more than one method in a progam and I have also never tried to count lines, words and letters in a file. So I've read three Java books file sections and then looked at questions on the forum here and had a go. I think I am doing something wrong with variable names, as I don't know the rules with where you put them in in multi method etc. If you could say something like that is glaringly wrong because you have to do this, that would be helpful. I can't use the debugger because I can't compile it. Anyway, thanks for looking at this.
import java.util.Scanner;
import java.io.*;
/**
* A program to count the number of lines, words and characters in a text file.
*
* #author
* #version 2014
*/
public class WordCount
{
public static void main(String[] args) throws FileNotFoundException
{
// get the filename
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
lines(filename);
words(filename);
characters(filename);
}
/**
* Count the number of lines in a file, and print out the result.
*
*/
public static void lines(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
file.nextLine();
}
return lines;
inputFile.close();
}
/**
* Count the number of words in a file, and print out the result.
*
*/
public static void words(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int words = 0;
while (input.hasNextLine())
{
String word = input.next();
words++;
}
return words;
inputFile.close();
}
/**
* Count the number of characters in a file, and print out the result.
*
*/
public static void characters(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int characters = 0;
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
for(int i = 0; i < line.length(); i++)
{ characters = line.charAt(i);
if (character != 32)
{
characters++;
}
}
}
return characters;
inputFile.close();
System.out.println("The number of lines is: " + lines);
System.out.println("The number of words is: " + words);
System.out.println("The number of characters is: " + characters);
System.out.println();
}
}
Change the following
public static int lines(String filename) throws FileNotFoundException
//change the return type to int instead of void
{
Scanner inputFile = new Scanner(filename); //filename instead of file
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
inputFile.nextLine(); // ---do----
}
inputFile.close(); // after return it will become unreachable
return lines;
}
Same applies to other methods
Your code contains several problems. So let us fix that one by one in each method:
public static void main(String[] args) throws FileNotFoundException
{
// get the filename
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String filename = keyboard.nextLine();
keyboard.close(); // <- don't forget to close this resource
File file = new File(filename);
//Scanner inputFile = new Scanner(file); // <- this line is unnecessary, because you never use that variable
lines(file); // <- you already obtained a File object, so use it ;)
words(file);
characters(file);
}
Next method:
public static void lines(File file) throws FileNotFoundException // mind the new argument type
{
Scanner inputFile = new Scanner(file);
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
inputFile.nextLine(); //<- use "inputFile" instead of "file"
}
inputFile.close();
//return lines; // <- you can't return something if your method uses "void" as the return type
//your comment on that method says that you like to print the result, so let's do that
System.out.println("The number of lines is: " + lines);
}
And the next one:
public static void words(File file) throws FileNotFoundException // <- new argument type
{
Scanner inputFile = new Scanner(file);
int words = 0;
while (inputFile.hasNextLine())
{
//String word = inputFile.next(); // that won't work like you think, but we can do a little "trick" here
String line = inputFile.nextLine(); // read the current line of text
words += line.split(" ").length; // split the line using whitespace and add the number of words to the current value of variable "words"
//words++; // <- not needed anymore
}
//return words; // <- like before: not possible if return type is void
inputFile.close();
//your comment on that method says that you like to print the result, so let's do that
System.out.println("The number of words is: " + words);
}
And the last method:
public static void characters(File file) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int characters = 0;
int count = 0; // new variable to count the characters
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
for(int i = 0; i < line.length(); i++)
{
characters = line.charAt(i); // <- no harm in using a new line :)
if (characters != 32) // <- you wrote "character"
{
count++; // <- "characters" is the current character itself, we won't increase that. For example "a" would become "b" and we don't want/need that :)
}
}
}
//return characters; // <- you know that already ...
inputFile.close();
//System.out.println("The number of lines is: " + lines); // <- "lines" and "words" are unknown here, but we printed these results already
//System.out.println("The number of words is: " + words);
System.out.println("The number of characters is: " + count); // <- print the count of characters
System.out.println();
}
After this changes you code should work now :).
It seems the methods only need the File object.
int n = countLines(file);
System.out.println("Lines: " + n);
...
public static int countLines(File textFile) throws IOException
{
Scanner inputFile = new Scanner(textFile);
int lines = 0
while (inputFile.hasNextLine())
{
lines++;
inputFile.nextLine();
}
inputFile.close();
return lines;
}
For nextLine you asked the wrong object, Scanner needed.
return returns from the method, so close has come before.
As you return the line count, you must return int.
Method names contain a verb by convention.
The method knows no file or filename.
It also cannot alter a variable if you pass a variable, like file,
You can give the new paramaeter the same name, but for clarity not done here.
Problem number 1:
public static void lines(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int lines = 0;
while (inputFile.hasNextLine())
{
lines++;
file.nextLine();
}
return lines; // <----------------
inputFile.close();
}
This is a void method but you have a return lines statement. Either remove it or change the method header to public static int lines(String filename) throws FileNotFoundException
Problem number 2
public static void words(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int words = 0;
while (input.hasNextLine())
{
String word = input.next();
words++;
}
return words; <-------------
inputFile.close();
}
Again here you ask from a void method to return a value. Change method header to public static int words(String filename) throws FileNotFoundException
Problem number 3:
public static void characters(String filename) throws FileNotFoundException
{
Scanner inputFile = new Scanner(file);
int characters = 0;
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
for(int i = 0; i < line.length(); i++)
{ characters = line.charAt(i);
if (character != 32)
{
characters++;
}
}
}
return characters; //<---------------
inputFile.close();
System.out.println("The number of lines is: " + lines);
System.out.println("The number of words is: " + words);
System.out.println("The number of characters is: " + characters);
System.out.println();
}
Again here you have a return int statement on a void method. Just change the method header to this: public static int characters(String filename) throws FileNotFoundException

Java simple counting words in a file

I am creating a simple program that counts the number of words, lines and total characters (not including whitespace) in a paper. It is a very simple program. My file compiles but when I run it I get this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at WordCount.wordCounter(WordCount.java:30)
at WordCount.main(WordCount.java:16)
Does anyone know why this is happening?
import java.util.*;
import java.io.*;
public class WordCount {
//throws the exception
public static void main(String[] args) throws FileNotFoundException {
//calls on each counter method and prints each one
System.out.println("Number of Words: " + wordCounter());
System.out.println("Number of Lines: " + lineCounter());
System.out.println("Number of Characters: " + charCounter());
}
//static method that counts words in the text file
public static int wordCounter() throws FileNotFoundException {
//inputs the text file
Scanner input = new Scanner(new File("words.txt"));
int countWords = 0;
//while there are more lines
while (input.hasNextLine()) {
//goes to each next word
String word = input.next();
//counts each word
countWords++;
}
return countWords;
}
//static method that counts lines in the text file
public static int lineCounter() throws FileNotFoundException {
//inputs the text file
Scanner input2 = new Scanner(new File("words.txt"));
int countLines = 0;
//while there are more lines
while (input2.hasNextLine()) {
//casts each line as a string
String line = input2.nextLine();
//counts each line
countLines++;
}
return countLines;
}
//static method that counts characters in the text file
public static int charCounter() throws FileNotFoundException {
//inputs the text file
Scanner input3 = new Scanner(new File("words.txt"));
int countChar = 0;
int character = 0;
//while there are more lines
while(input3.hasNextLine()) {
//casts each line as a string
String line = input3.nextLine();
//goes through each character of the line
for(int i=0; i < line.length(); i++){
character = line.charAt(i);
//if character is not a space (gets rid of whitespace)
if (character != 32){
//counts each character
countChar++;
}
}
}
return countChar;
}
}
I can't really say the exact reason for the problem without looking at the file (Maybe even not then).
while (input.hasNextLine()) {
//goes to each next word
String word = input.next();
//counts each word
countWords++;
}
Is your problem. If you are using the input.hasNextLine() in the while conditional statement use input.nextLine(). Since you are using input.next() you should use input.hasNext() in the while loops conditional statement.
public static int wordCounter() throws FileNotFoundException
{
Scanner input = new Scanner(new File("words.txt"));
int countWords = 0;
while (input.hasNextLine()) {
if(input.hasNext()) {
String word = input.next();
countWords++;
}
}
return countWords;
}
I have just added an if condition within the while loop. Just make sure to check there are token to be parsed. I have changed only in this place. Just make sure to change wherever needed.
This link will give good info. in regard to that.
Hope it was helpful. :)

Categories