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

Thread: Sets

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sets

    Hi Ladies and Gents,

    I'm trying to teach myself about sets from scratch and the uses of them and I have thought of a method that could be useful.

    If I had a method called for example displayWordsInString() which takes a string argument called text.

    Could I get this method to return a set consisting of words that are in the string referenced by the argument.

    So myClass.displayWordsInString(" the train is late again ");


    should print out an array:



    [late,train,is,again,the]


    the order of print out is not really that important to me!!


    If there is a solution to this I could try out, it would really stretch my knowledge a bit , thanks again in advance to this great learning forum!!.


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

    Default Re: Sets

    Look at the String class's split method. It will help you extract the words from the string.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sets

    String [] text =  ["late" ,"train", "is", "again", "the"];
     
    text.split(",");
     
    for ( int i = 0; i < text.length; i++)
     
    {
      System.out.println(text[i]);
    }

    I get the following error
    Semantic error: line 1. Message split( java.lang.String ) not understood by class'[Ljava.lang.String;'

    Can you put me out my misery Norm and correct my code.... thanks a million.

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

    Default Re: Sets

    Semantic error: line 1. Message split( java.lang.String ) not understood by class'[Ljava.lang.String;'
    Read the API doc for the split() method. What does it take for an argument?
    What argument did you give it in the code you posted?

    [Ljava.lang.String
    That is how the compiler refers to a String array.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sets

    split(String regex, int limit)

    These are the arguments in the API but to be honest I dont know what they mean or how to implement them.

    rGds

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sets

    The doc doesn't quite tell how to code it
    String [] text = {"late" ,"train", "is", "again", "the"};
     
    text.split(",");
    The problem with your code is that text is not a String.
    Its an array of String. Different.

    Also the split() method returns an array which your code doesn't receive.

    This should compile:
    String[] aStrArray = text[0].split(",");
    The first element of the text array would be split into the array: aStrArray.
    To show the contents of that array:
    System.out.println("aStrArray=" + java.util.Arrays.toString(aStrArray));
    Last edited by Norm; July 17th, 2011 at 02:06 PM. Reason: Changed []s to {}s for array definition

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Sets

    Quote Originally Posted by av8 View Post
    split(String regex, int limit)

    These are the arguments in the API but to be honest I dont know what they mean or how to implement them.
    The 'limit' argument is optional (there is an overloaded method that takes just a regex String). The API docs try to tell you what you need to know to use the method. If you don't know what a 'regex' or 'regular expression' is, follow the link.

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sets

    I have tried String [] text = {"late" ,"train", "is", "again", "the"}; with the curlys instead of []

    text.split(",");

    and still get an error do i need to use import java.util.*;

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sets

    I tried to explain your problem in post #6. Did you have any questions about what I said there.
    Did you try the code I posted?

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sets

    Yes thankyou Norm it worked A OK.

Similar Threads

  1. How to use Sets
    By copeg in forum Java Programming Tutorials
    Replies: 1
    Last Post: September 29th, 2010, 11:07 AM
  2. How to use Sets
    By copeg in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: September 29th, 2010, 11:07 AM
  3. [SOLVED] Maps and adding to sets as values
    By mds1256 in forum Collections and Generics
    Replies: 3
    Last Post: March 26th, 2010, 09:12 AM
  4. [SOLVED] Sets and creating a new set containing a common number
    By mds1256 in forum Collections and Generics
    Replies: 2
    Last Post: February 26th, 2010, 06:00 PM
  5. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM