aspose file tools*
The moose likes Beginning Java and the fly likes Scan a text file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scan a text file." Watch "Scan a text file." New topic
Author

Scan a text file.

john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
What would i do to scan a text file for a certain line of text, and notate down how many times it has it. like would probably do something like:


I'm not sure where to go from there though.

[ November 30, 2008: Message edited by: john larry ]
[ November 30, 2008: Message edited by: john larry ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

John,
If you are on Java 5 (and this isn't a school project), you can use the Scanner class. If not, you'll have to read the lines of the file yourself. Either way, you need to figure out how to express what you want to match in Java.

If you share which approach you plan to take (scanner vs read file), you may get some more concrete advice.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Go through the regular expressions tutorial; you can probably find whether the pattern you are looking for is matched in the file, then whenever it is you can increment your counter.

You might do well to look at the "quantifiers" section; depending on what you are looking for, you might get a different count with a greedy quantifier from a reluctant quantifier.

Other people will doubtless have other ideas about this question.
john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
This is for school, but it is using the Scanner class. Here is all that i have so far:



But, as you can see i can't think of how to scan the text file "test1.txt" for the three different words and mark down how many times they occur.
jaywee
Greenhorn

Joined: Oct 31, 2008
Posts: 2
I suppose you've known the line you want to check in the file.so here is my code:

wish can help you!
john larry
Greenhorn

Joined: Nov 23, 2008
Posts: 9
My code:


I'm getting this error when i run it, it compiles fine:

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1471)
at Family.main(Family.java:19)
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1471)
at Family.main(Family.java:19)
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1471)
at Family.main(Family.java:19)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

jaywee, please check your private messages for an administrative matter. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You can get NosuchElementExceptions if the Scanner tries to read more lines than there are in the text file. Remember there are Scanner methods for reading all sorts of different data types directly from text.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scan a text file.