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 5 of 5

Thread: Help with array of Strings...Linear Search?

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Help with array of Strings...Linear Search?

    Hello everyone,

    I've recently been introduced to arrays in my java module and i'm stuck on a 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

    Could anybody explain to me how I would go about checking the array to see if each word has occurred already? Is linear searching the best route to take? Thanks for any help given. Oh and class Console is just a more convenient version of ConsoleReader()

    Even though this is probably all wrong, here is my attempted code so far:

     
    public class DistinctWords
    {	
    	public static void main(String[] args)
    	{
    		String[] words = new String[10]; // Allow up to 10 words 	
    		int count=0;
    		int distinctWord=0;
    		System.out.println("Enter sentence:");
    		while(!Console.endOfFile())
    		{
    			words[count]=Console.readToken();
    			count++;
    		}
                    int i=1;
    		while(i<count && !words[i].equals(words[i-1]))
    		{
    			i++;
    		}
    		if(i<count) // found
    			System.out.println("Found");
    	}//end main
    }//end class
    Last edited by Stockholm Syndrome; March 18th, 2011 at 04:54 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with array of Strings...Linear Search?

    A Map or Set would be perfect for this, but perhaps not the purpose of the question. There are many ways to go about doing this. Write out the problem by hand - you have an array you'd like to loop over to check for duplicates, how would you do this?

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with array of Strings...Linear Search?

    Thanks for the reply.. but that's what im unsure of? I don't know how I would check for duplicates? So far in my module, we've only been shown linear search but that stops searching after the element is found. For example in my attempt above, the program will stop searching once its found a word that matches the previous word. I'm really confused atm sorry

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with array of Strings...Linear Search?

    Break the problem down to the basics - you need something to loop, something to check equality, something to count - but one thing at a time...write it down, and look at it visually (often time helps). You've got an array

    0 1 2 3 4 5 6 7 8 9


    10 elements in the array. How would you iterate over this array? How would you check things are equal? Once you've got that down, how would you count (how about create a new class that contains the string and count)?

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with array of Strings...Linear Search?

    Thanks for the help i eventually solved it Cheers

Similar Threads

  1. filling an array with strings
    By exose in forum Collections and Generics
    Replies: 3
    Last Post: February 18th, 2011, 08:44 AM
  2. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 09:47 PM
  3. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM
  4. [SOLVED] Interpolation Search for Strings ??
    By george in forum Algorithms & Recursion
    Replies: 3
    Last Post: May 18th, 2010, 06:11 AM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM

Tags for this Thread