Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scan a text file.

 
john larry
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaywee, please check your private messages for an administrative matter. You can see them by clicking My Private Messages.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic