Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with Arrays - Counting elements

  1. #1
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with Arrays - Counting elements

    Hello everyone hows it going

    I've only recently been introduced to arrays and require a bit of help with a small exercise I was doing earlier. Here is the question :

    Write a program which reads a sequence of words, and prints a count of the number of distinct words. An example of input/output is

    should I stay or should I go
    5 distinct words

    I have solved the question below, without using an array but I presume an array is what the question requires as the exercises are related to the sections being studied. Could anybody show me how I would use one in my solution instead?

     
    public class Ex1
    {
    	public static void main(String[] args)
    	{
    		System.out.println("Enter Sentence:"); 
    		int count=0;
    		while(!Console.endOfFile())
    		{
    			String word = Console.readToken();
    			if(word.length()> 1)
    			{
    				count++;
    			}		
    		}
    		System.out.println("Number of distinct words is " + count);
     
    	} // end of main
    } // end of class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Arrays - Counting elements

    I have solved the question below
    You can't solve the problem without saving what has been read previously to compare against.
    What is your count for this: This cat and This dog and This other dog

    To use an array, you have to assume a maximum number of possible words will be entered.
    Create an array of that size.
    Have an index to the array for adding new elements to the array.
    when you get a new word, scan the array to see if its there, if not add it.

    At the end all the unique words will be in the array.
    Last edited by Norm; August 6th, 2010 at 04:56 PM.

  3. #3
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays - Counting elements

    Quote Originally Posted by Norm View Post
    You can't solve the problem without saving what has been read previously to compare against.
    What is your count for this: This cat and This dog and This other dog
    Count is 9. Its a distinct word if there is more then one letter. Im aware i havnt completely solved the question as i need an array. But how do I use one in a loop to check each word I enter to see if its length is greater than 1?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Arrays - Counting elements

    Its a distinct word if there is more then one letter
    Are you sure about that definition? It doesn't make sense as an assignment.
    I would think distinct meant the same as unique.

  5. #5
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays - Counting elements

    Quote Originally Posted by Norm View Post
    Are you sure about that definition? It doesn't make sense as an assignment.
    I would think distinct meant the same as unique.
    haha it would seem ive totally misinterpreted the question. I'm such a noob. Sorry for wasting your time

    Im gonna attempt it now, knowing what a distinct word actually is. I'll keep this thread unsolved incase i encounter another problem because im still trying to get my head around arrays

    Cheers

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Arrays - Counting elements

    Creating an array:

    // an array of strings, with 10 elements
    String[] list = String[10];

    Getting/setting the element of an array:

    // set
    list[0] = "Hello world!";
    // get
    String theString = list[0];

    Getting the length of an array:

    int length = list.length;

    You can compare two strings using the equals method.

    String string1 = "Hello world";
    String string2 = "not equal";
    String string3 = "Hello world";
    boolean same = string1.equals(string2); // same = false
    same = string1.equals(strin3); // same=true

    Once you create an array, it's size becomes fixed and can never be changed. To "change" the size of an array, you must create a new array of the size you want, then copy over the elements to the new array.

  7. #7
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays - Counting elements

    Sorry but I'm still unsure as to how to keep going through the array and checking if a word is unique or not. All i know is that it's probably done in the for loop below with the question marks. Could anybody help me, i'm still confused with these

    public class Ex1
    {
    	public static void main(String[] args)
    	{
    		String[] words = new String[10];
    		System.out.println("Enter Sentence:");
    		for(int i=0; i<words.length; i++)
    		{
    			words[i] = Console.readToken();
    		}
    		for(int i=0; i<words.length; i++)
    		{
    	                 ???
    		}		
    	}// end main
    }// end class

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Arrays - Counting elements

    Your posted code shows that you read words one after another into an array.

    How would you do it if you were to do it manually with words written on a piece of paper?
    Look at the next piece of paper, have I seen this word yet?
    No, count it
    Yes skip it

    The trick here is to remember that you have seen the word before. Think about ways to do that

Similar Threads

  1. Question: counting
    By miss confused in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2010, 05:38 PM
  2. Program help with counting even and odd integers in input value.
    By fueledbyrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2010, 07:02 PM
  3. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  4. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM

Tags for this Thread