• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scan a text file.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic