Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need 2 read text file & count characters

Status
Not open for further replies.

puntito

Programmer
Mar 22, 2006
18
Hi:

I need to do a program wich reads a text file.
a) count the number of words that the text have (a word is any secuence of characters, not containing space blankets, tabs or end lines).
b) Count the number of lines.
c) Replace a String with another one, sended like a parameter.
c) Send the result to the standard output.

Ohh God¡¡¡

I don't have an idea of how to read the file.

a) b)
String text="I love my dog";
String chk= new String();
int countr=0;
int cntln=0;
int x=text.length();
for (int i=0;i<x;i++)
{
if (text==" " && text=="/n" && text="/t")
countr++;
if (text=="/nl" )
{ cntln++;
countr++;}
}

c) d)

String str1="I hate my old car";
Strin str2= new String();
str2= str1.replaceAll("hate", "love");

System.out.println(str2);


Is this correct??
Is there a different way to do it???
Do I need to correct something??
Help me please¡¡¡¡
 
Google for "read file java" - loads of hits.

Look at the java.io package, specifically the BufferedReader class - using the readLine() method.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi !

There are a couple of things to consider:
- Is the file organized with LF / return or is it "one big string" ?
- Are word written in a tidy way - that means only one blank as a delimiter, not multiple blanks instead of tab, like stupid secretaries love to generate with Word/Wondows?

Lets presume you have no special needs. Then write a "write once, run every time"-solution! Reusable code is very fashionable nowadays.

Use a BufferedReader to read lines
br.readLine()
Read your line into a StringBuffer, not a String for reasons you should understand (waste memory, waste runtime...) - so counting lines is already easy to do.

Scan your StringBuffer char by char - interpreting special characters like tab or disallowing double blanks or whatever may suite your needs. Whatever you read in - doing so you can transfer it to a "correct" format.

Words are those items that are delimited by blanks or special characters (tab, LF, ret...).
You can do the counting "on the fly" or put every single item out to a List (Vector, ArrayList...), then get the number of words by list.size() - counting is easy again.

For a learning effect look at StringBuffer and StringBuilder. You can define delimiting characters and get the results tokenized - nearly good enough to use for just anything. But if you want COMPLETE control, write your own input class.

What are the benefits of doing it using your own "complicated" input class?
I have written very few classes with some degree of generalization - maybe a little more than was necessary for the actual problem. But I am using them since. It is not the programming that takes time. IT IS TESTING, whenever things are not running the way you want.
By now I could largely reduce my testing efforts by writing general solutions that ere MINE - meaning I can control and anytime change what happens.

Have fun playing with... (whatever you want - let's presume it will be) JAVA !
 
Forgot something to mention:

A word in ordinary text is probably delimited by other characters, too!
This can be: . ! ? " ' spanish reversed question mark etc.
If you do the counting, you have to consider that!

If you use StringTokenizer (ATTENTION: my reply above has a typical problem: I still had StringBulder/StringBuffer in mind, but for the single tokens you need StringTokenizer, please correct the respective paragraph !)
you need to define all the possible delimiters, otherwise your count produces wrong results. Sometimes difficult to know in advance - my solution: write a generalized class, that handles these problems!)

So far, so bad. Code still has to be written.

To show you the benfits of controling the filtering of special characters, here is my example. It will replace everything you don't expect as a text character (and is not really complete code, of course you have to open a file ...etc...) I hope it is readable and serves as an example of what you could do. Here I am reading char by char on the physical data read level, organizing my results to a line that can be used as one text line - if needed. Depends on what you need for your solution.

private StringBuffer my-own-readline()
{ int z = 0; char c = ' ';
int count = 0;
// end of line
( calling prog:
StringBuffer line = new StringBuffer(); )
line.setLength(0);
boolean eof = false;
boolean eol = false;

while ( !eol )
{ try
{ z = input.read();
//System.out.println(" Z "+z);
switch (z)
{ case -1: { z=32; eof = true;
eol = true;
break; }
case 10: { z=32; eol = true; break; }
case 13: { z=32; break; }
case 9: { z=32; break; }
case 255: { z=32; eof = true;
eol = true; break; }
default: { c = (char)z;
//System.out.println(" C "+z);

line.append(c);
//System.out.println(" line "+line); count++;
}
} // end switch

} // end try part
catch ( IOException e )
{ System.out.println("\nProblem reading
data...\n" ); return false; }
}
//System.out.println("readline "+line);

return line;

This code reads and converts one line - doing the "filtering". You need to run it in a loop until EOF, which is detected in the code - just look, hope you can figure out yourself how to communicate this information with the calling code (return parameter, boolean flag, whatever... )!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top