Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,359 people online right now. Registration is fast and FREE... Join Now!




Counting the target word

 
Reply to this topicStart new topic

Counting the target word

kru
6 Sep, 2008 - 05:05 PM
Post #1

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 24


My Contributions
I'm not getting the right number of times that the target string occurs in the given array. I think the problem is in the for loop of the count method but I'm not sure what it is or why.

CODE

public class Counting
{
    public static void main (String[] args)
    {
        //Declar array
        String array[] = {"three", "three", "two"};


        //print array
        System.out.println("three, three, two");


        //compute and print the count number

        int trackCount = count(array, "three", array.length);
        System.out.println("The word \"three\" appears " + trackCount + " times.");


        //Finally, print the results to the console window.
    }

    public static int count (String[] values, String target, int arrayLength)
    {  
        int counter=0;
        for(int i = 0; i < values.length; i++){
            if (values.equals(target))
            counter++;

    }
    return counter;

    }
}

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Counting The Target Word
6 Sep, 2008 - 07:30 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
You need to subscript values so that it compares the string at a particular element to the target string. Right now it's comparing the entire array against your target string.
On an unrelated note, the arrayLength parameter is unnecessary since Java arrays know their own length, and you don't use it anyway.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Counting The Target Word
6 Sep, 2008 - 07:32 PM
Post #3

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,312



Thanked: 102 times
Dream Kudos: 1275
My Contributions
Your problem is that you have an array of strings and are treating it like a single string, try this:

CODE
public class Counting{
    public static void main (String[] args){
        //Declar array
        String array[] = {"three", "three", "two"};
        //print array
        System.out.println("three, three, two");
        //compute and print the count number
        int trackCount = count(array, "three”);
        System.out.println("The word \"three\" appears " + trackCount + " times.");
        //Finally, print the results to the console window.
    }
    public static int count (String[] values, String target){  
        int counter = 0;
        for(int i = 0; i < values.length; i++){
            if (values[i] == target){
                counter++;
            }
        }
        return counter;
    }
}

User is online!Profile CardPM
+Quote Post

kru
RE: Counting The Target Word
7 Sep, 2008 - 10:11 AM
Post #4

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 24


My Contributions
Ok, thanks. It works now. Also with strings, you can't use "==" to compare objects. You have to use the .equals function.
User is offlineProfile CardPM
+Quote Post

bbq
RE: Counting The Target Word
9 Sep, 2008 - 12:34 AM
Post #5

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 200



Thanked: 17 times
Dream Kudos: 50
My Contributions
QUOTE(kru @ 7 Sep, 2008 - 11:11 AM) *

Ok, thanks. It works now. Also with strings, you can't use "==" to compare objects. You have to use the .equals function.


Just one other thing, i would move away from the static stuff, heres how i would do the program

CODE
public class Counting
{
    
         private String array[] = {"three", "three", "two"};

      void run()
      {
        //Declar array
              //String array[] = {"three", "three", "two"};
        //print array
             for (int i = 0; i < array.length; i++)
        {
            System.out.println(array[i] + ", ");
        }


        //compute and print the count number
        int trackCount = count("three");

            System.out.println("The word \"three\" appears " + trackCount + " times.");


      }

         public int count (String target)
         {  
          int counter=0;

           for(int i = 0; i < array.length; i++)
        {
                if (array[i].equals(target))
                      counter++;

        }
            return counter;

         }


          public static void main (String[] args)
        {
                Counting myProgram = new Counting();
                     myProgram.run();        
        }
}


Hope it helps a bit more smile.gif

This post has been edited by bbq: 9 Sep, 2008 - 12:38 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month