Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

text file  RSS feed

 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am having problem in fixing my course work.here is the question:
i am to design,implement and demonstrate a command-line application
that count the number of words,sentences,paragraphs,digits in a piece
of input text.pls do assist me,thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

We'll gladly try to get you going. What do you have so far? Are you familiar with Java file I/O in general? If not, here's an example of how to read a file line by line. Once you have read the text, how would you count it?

I'll move this over to the I/O and Streams forum where file I/O questions are handled.
 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i try what is not just going,i want to design first before applying but is not going.i dont know what to use to read a file and count the number of paragraphs,words,sentences,punctuation mark,letter,digits and the analysis report should be output to the standard output stream.what should i use for counting?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reading a file the code I linked to above is a good start. Make sure you understand that before moving on.

In order to count words, digits etc. you need to come up with a definition of what constitutes a word/sentence/paragraph. In other words, which characters in the text do you need to look for in order to separate one word/sentence/paragraph from the next?

The code above gives you each line of the file as a String. You can inspect individual characters within a String using the charAt method. As an example, you'd have a digit in the file if charAt returns something between '0' and '9'.
 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am lost.but this is what i have
(last week we work on dice game.
we noted the problem,thank you.)this is my file named LastWeek.txt
my source code is
import java.io*;
import java.util.*;
public class LastWeek
public static void main(String[] args)
BufferedReader br=new BufferedReader(new FileReader(new File(LastWeek.txt)))
word w = number of word = nW
word w = int nW;
sentence sn =number of sentence=nSn
sentence sn=int nSn;
paragraph pg=number of paragraph=nPg
paragraph nPg=int nPg;
is that what you mean?and pls if there is anything do correct me.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code doesn't seem to do anything, does it? It's just a few variable declarations as far as I can tell. You'll need those, for sure, but I would start by writing code that examines each character in the file. Then you can think about when to increment each of those variables.
 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am completely lost,this is my first text design i dont know how to define the character and make it is a little bit confusing but i have an idea can i use stream tokeniser?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont know how to define the character

Well, this is an assignment, right? If you're not sure what you're being asked to do, ask your profesor or TA to clarify.

can i use stream tokeniser?

I think that would work. Here's an example of StreamTokenizer in action.
[ November 21, 2007: Message edited by: Ulf Dittmer ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try one thing at a time. Planning everything in advance is overwhelming and ignores the thought that you might learn a better way to do things along the way.

In fact, let's start with doing no things from the assignment. Make this say "true". You can put this code in another class that you might or might not turn in when you're done.

For that you have to write a new class MyFileAnalyzer (or whatever you want to call it) and write a getLineCount() method that just returns 0. That's well under 10 lines and makes sure you're comfortable with those steps.

Then make this say "true" with a one line test file:

For that you have to add code to read the file - you can lift that from the sample link above - and count the lines.

If you take tiny steps you can have many small victories which is fun, and show us exactly where you're stuck, if & when you are.
[ November 22, 2007: Message edited by: Stan James ]
 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
import java.util.*;


public class Star
{
public static void main(final String[] args) throws IOException
{
int idx = 0; int tokenCount;
String words[] = new String [];
String message="The text of the message to be broken up for analysis";
StringTokenizer st = new StringTokenizer(TwinkleStar);
tokenCount = st.countTokens();
get (Words)
System.out.println("Number of tokens = " + tokenCount);
while (st.hasMoreTokens()) // make sure there is stuff to get
{ words[idx] = st.nextToken(); idx++; }
for (idx=0;idx<tokenCount; idx++)
{ System.out.prin

BufferedReader in =
new BufferedReader(new FileReader("TwinkleStar.txt"));
get TwinkleStar
string[] data =new String[]
tln(words[idx]); }
}
}
public static int countLetters( String s )
{
if( s == null )
{
return 0; // defensive programming THIS IS WHAT I HAVE AND IS NOT GOING ,HAVE LIKE 15 ERRORS
 
Val�ry Urbain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aqeel,
Let me ask you this: Can you understand a single line of what you posted?
If so, please explain in your own words what it does.
 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileInputStream inRaw = new FileInputStream("TheRead.txt");
int ch;
int countWords = 0;

while (true) {
ch=inRaw.read();
if (ch == ' '
&& previousch != ' ')
{

countWords ++;



System.out.print((countWords) ch);


}


}

}
pls can somebody push me forward i am getting 1 error cant figure why
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting? In which line of the code?
 
Am Aql
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at line 34:reached end of filewhile parsing }^ thats what is showing at system.out.println
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code you're using; it's hard to say what might be going wrong without seeing it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!