Results 1 to 9 of 9

Thread: wrong values

  1. #1
    mark-mlt is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Exclamation wrong values

    i want to get the number of words loaded from an array: i did this >

    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();
    			}
    		}
    but it is overwriting the sentece every time so it is giving me the wrong value. any help is welcom :)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mark-mlt View Post
    i want to get the number of words loaded from an array

    What you mean here, want to count words in an arrray, or counts words in a line of text?

  3. #3
    mark-mlt is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    the line of a text

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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());

  5. #5
    mark-mlt is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    yes that works, but i want to populate

    String str = "Count numner of words here.";

    from an array

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  7. #7
    mark-mlt is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    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());
    			}
    		}

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not clear how you are handle some variables. Specially what you try to do here?

    Java Code:
    String lineContent = new String();
    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:
        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());
            }
        }

  9. #9
    mark-mlt is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Thumbs up

    thx alot anyway ;) i managed to do it :) it was hard for a begginer :D.

Similar Threads

  1. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 PM
  2. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM
  3. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  4. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •