Counting Words and Characters in a Text File: A Java Program

In summary: I found a solution that works. I changed the code to:public class DocumentS{ public static void main(String[] args) { In in0 = new In(args[0]); int wordCount = 0; int charCount = 0; while (!in0.isEmpty()) { String str = in0.readString(); for (int i = 1; i <= str.length(); i++) charCount++; } System.out.println("Number of words: " + wordCount);
  • #1
Hiche
84
0

Homework Statement



Write a program that takes a file name as argument and counts the number of words and the number of characters (without spaces) in the document.

Homework Equations


The Attempt at a Solution



How exactly should my program take a .txt file as an argument? Do I simply:

Code:
String str = args[0];

And the contents of the .txt file are stored as strings? (The file is a text file with only letters).

And how to check for each word inside that file? Let it read the string till encountering a space then (by previously declaring a count variable) increment count? I'm confused here. Is it that same as just studying a string?
 
Last edited:
Physics news on Phys.org
  • #2
You can count the numbers of words by reading the lines from the .txt file and splitting them using String Tokenizers.


http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
 
Last edited by a moderator:
  • #3
Sadly, we are limited to using the In class for this program. I came up with this but it is not working:

Code:
public class DocumentS
{
	public static void main(String[] args)
	{
		In in0 = new In(args[0]);
		
		int wordCount = 0;
		int charCount = 0;
		
		while (!StdIn.isEmpty())
		{
			wordCount++;
			String str = in0.readAll(); 
			for (int i = 1; i <= str.length(); i++)
				charCount++; 
		}
		System.out.println("Number of words: " + wordCount);
		System.out.println("Number of characters: " + charCount);
	}
}

It compiles fine but when I pass the .txt file as an argument, nothing happens. I don't know if the algorithm is right, but I really need help in this.
 
  • #4
I do see some problem in the above code but i don't know what exactly, so sorry. Also, i don't know much about "In class", although i found something on "In.class" online and modified it a bit to count the numbers of words and characters in a file and it works fine. Would you like to know about that? or you would like to stick to your version and fix it?
 
  • #5
I saw there are functions like
readString() // it reads one word at a time
readChar() // it reads one character at a time



Therefore use these functions.
Also take filename as follows

String file = args[0];
In in0 = new In(file);
 
  • #6
Hiche said:
Sadly, we are limited to using the In class for this program.
I don't believe that the In class is present in standard Java, but is a class that your instructor provides for you to use. Since you are limited to using just this class, you need to understand the methods and properties that are exposed by this class. We can't point you in the right direction without knowing what things are in this class.
 
Last edited:
  • #7
Code:
public class DocumentS
{
	public static void main(String[] args)
	{
		In in0 = new In(args[0]);
		
		int wordCount = 0;
		int charCount = 0;
		
		while (!in0.isEmpty())
		{
			wordCount++;
			String str = in0.readString(); 
			for (int i = 1; i <= str.length(); i++)
				charCount++; 
		}
		System.out.println("Number of words: " + wordCount);
		System.out.println("Number of characters: " + charCount);
	}
}

I managed to fix this (I changed StdIn.isEmpty() to in0.isEmpty() since we are looping until the end of the file we attached rather than wait for no input which is StdIn). The In class is part of an external library our instructor provided for us. I though many people knew about it, but I guess I was wrong. The library is in this link if anyone is remotely interested: http://www.sendspace.com/file/t7wqmp

Thanks for the replies.
 

Related to Counting Words and Characters in a Text File: A Java Program

1. What is a .txt file in a Java program?

A .txt file is a plain text file that contains human-readable text, without any special formatting or coding. In a Java program, it can be used to store data or to read data from external sources.

2. How do I create a .txt file in a Java program?

To create a .txt file in a Java program, you can use the FileWriter class, which allows you to write data to a file. You can also use the PrintWriter class to write data to a .txt file in a specific format.

3. How do I read data from a .txt file in a Java program?

To read data from a .txt file in a Java program, you can use the FileReader class, which allows you to read data from a file. You can also use the Scanner class to read data from a .txt file in a specific format.

4. Can I edit a .txt file in a Java program?

Yes, you can edit a .txt file in a Java program by using the FileWriter class to write data to the file. You can also use the RandomAccessFile class to read and modify specific parts of the .txt file.

5. How do I handle errors when working with .txt files in a Java program?

You can handle errors when working with .txt files in a Java program by using try-catch blocks to catch any exceptions that may occur. You can also use the throws keyword in your method signature to pass the responsibility of handling the exception to the calling method.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top