Results 1 to 9 of 9
Thread: wrong values
- 04-25-2008, 04:31 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
wrong values
i want to get the number of words loaded from an array: i did this >
but it is overwriting the sentece every time so it is giving me the wrong value. any help is welcom :)Java Code:public static void countWords() { FileLoader.fileLines(); String lineContent = new String(); for (int i = 0; i < fileLines.size(); i++) { // get a line of text from the file { lineContent += String.format("%s", fileLines.get(i)); String str = lineContent; StringTokenizer st = new StringTokenizer (str); System.out.println("number of wds: " +st.countTokens()); str.isEmpty(); } }
- 04-25-2008, 04:37 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-25-2008, 08:58 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
the line of a text
- 04-25-2008, 09:11 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Why you doing too much things there? I think there is a space between each two words. Using StringTokenizer, you can do it easily. Try the following.
Java Code:String str = "Count numner of words here."; StringTokenizer st = new StringTokenizer(str, " "); System.out.println(st.countTokens());
- 04-25-2008, 09:54 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
yes that works, but i want to populate
String str = "Count numner of words here.";
from an array
- 04-25-2008, 10:04 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes you can, but the problem is defining the array length. Because the length should be a constant value. So the solution is List or ArrayList. Do you how to work on with them?
- 04-25-2008, 10:09 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
the text file i am reading from:
one two three four five six seven
one two three four five six
one two three
one two
one
-------------------------------------------------------------------
by the code below, im counting the words but giving me wrong values.
-------------------------------------------------------------------
Java Code:public static void countWords() { FileLoader.fileLines(); String lineContent = new String(); for (int i = 0; i < fileLines.size(); i++) { // get a line of text from the file { lineContent += String.format("%s", fileLines.get(i)); String str = lineContent; StringTokenizer st = new StringTokenizer (str); System.out.println("number of wds: " +st.countTokens()); } }
- 04-25-2008, 10:20 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm not clear how you are handle some variables. Specially what you try to do here?
Try the following, it's not difficult if you get correctly what I have post with in my last code. ;) Only thing you have to do is, looping through the all strings you have.Java Code:String lineContent = new String();
Java Code:public void countWords() { String[] tens = {"one two three four five six seven", "one two three four five six", "one two three", "one two", "one"}; for(int i = 0; i < tens.length; i++) { StringTokenizer st = new StringTokenizer(tens[i], " "); System.out.println("number of wds: " + st.countTokens()); } }
- 04-25-2008, 11:11 AM #9
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:16 PM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks