Results 1 to 16 of 16
- 05-15-2011, 06:37 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
Counting occurence of a word in file
I can't seem to get the current code to count all occurences of specified word in the file. It should display 10, but only counts 8. Any obvious reason why it doesnt do this properly?
Java Code:import java.io.*; class lestal { public static void main(String[] args) throws Exception { FileReader fil = new FileReader("d:\\lab9_3.txt"); BufferedReader br = new BufferedReader(fil); StreamTokenizer st = new StreamTokenizer(br); String SearchFor = "Java", word = ""; int input, counter = 0; while ((input = st.nextToken()) != StreamTokenizer.TT_EOF) { if ((input == StreamTokenizer.TT_WORD)) { word = st.sval; } if (word.compareTo(SearchFor) == 0) { System.out.println(st.sval); counter++; } } System.out.println("Fant ordet \"Java\" " + counter + " ganger."); fil.close(); } }
Last edited by KAS; 05-15-2011 at 06:40 PM.
- 05-15-2011, 07:20 PM #2
Try debugging the code by adding print statements to show what tokens are being read and what Strings are being compared. The output should show you where the problem is.
What does the input file contain?Last edited by Norm; 05-15-2011 at 07:22 PM.
- 05-15-2011, 07:54 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
Input file is equal to the text here: Primitive Data Types The Java programming language is statically-typed, which m - Pastebin.com
- 05-15-2011, 07:58 PM #4
How does the program work with other input?
- 05-15-2011, 08:04 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
If I just type "Java" 13 times in the same file(removing the rest), it counts all 13. So it seems the problem is located elsewhere.
- 05-15-2011, 08:09 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-15-2011, 08:13 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 13
Just a reminder about StringTokenizer - the API docs say "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."
For some reason they haven't deprecated it yet, but the sentiment is similar.
- 05-15-2011, 08:19 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
- 05-15-2011, 08:23 PM #9
So have you tried debugging it using print statements to see where the problem is?
- 05-15-2011, 08:48 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
It seems to skip here(marked in bold)
Primitive Data Types
The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as you've already seen:
int gear = 1;
Doing so tells your program that a field named "gear" exists, holds numerical data, and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed on it. In addition to int, the Java programming language supports seven other primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.Last edited by KAS; 05-15-2011 at 08:54 PM.
- 05-15-2011, 08:58 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
If the file will always be small simply read the entire file into a String and then use indexOf(String, int) in a loop with a increment variable.
- 05-15-2011, 09:09 PM #12
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
Tried to write the code a bit different:
Java Code:import java.io.*; class OcccurenceCounter { public static void main(String[] args) throws Exception { FileReader fil = new FileReader("d:\\lab9_3.txt"); BufferedReader br = new BufferedReader(fil); StreamTokenizer st = new StreamTokenizer(br); String SearchFor = "Java", word = ""; int input, counter = 0; do{ do{ input = st.nextToken(); } while (input != StreamTokenizer.TT_WORD); word = st.sval; if(word.matches(SearchFor)){ System.out.println(word); counter++; } } while (input != StreamTokenizer.TT_EOF); fil.close(); System.out.println("Fant \"Java\" " + counter + " ganger."); } }
- 05-15-2011, 09:09 PM #13
There are many methods to call to control how StreamTokenizer works.
Like ordinaryChar or quoteChar.
Have you tried some of those?
- 05-16-2011, 07:23 PM #14
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
It seems the StreamTokenizer is bugged somehow, so I'm trying to solve this in another way.
- 05-16-2011, 07:33 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-16-2011, 07:47 PM #16
Similar Threads
-
please help with counting specific word in a sentence
By noobinoo in forum New To JavaReplies: 4Last Post: 05-07-2010, 03:06 PM -
Counting specific word from a file
By jaq in forum New To JavaReplies: 2Last Post: 12-02-2009, 07:12 PM -
Searching the first occurence
By The Hawk in forum New To JavaReplies: 7Last Post: 11-29-2009, 01:36 PM -
count occurence of word in a line of text
By sinyi88 in forum New To JavaReplies: 19Last Post: 02-28-2009, 08:37 AM -
[SOLVED] Help on Word and Character counting in java
By Alistair in forum New To JavaReplies: 2Last Post: 05-15-2008, 04:48 AM
Bookmarks