Results 1 to 3 of 3
  1. #1
    romanrsr is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Question Comparing String To Array Element ?

    I have the following code:

    Java Code:
    public class Equals {
    
            String[] s1 = {"red", "white", "black", "blue"};
            String[] s2 = {"red", "black", "green"};
            String[] s3 = {"red", "green"};
    What I need is to give me the following output:

    Select Strings:
    s1,s2,s3

    Comparing String s1, s2, s3

    red 3 matches.
    black 2 matches.
    green 2 matches.

    Someone can help me?

    Thanks a lot!

    Greetings!
    Last edited by romanrsr; 10-17-2014 at 04:44 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Comparing String To Array Element ?

    I am still not certain what you want to do. What are you comparing? It sounds to me like
    you are doing a frequency count on the colors in the supplied string arrays. Why are you using three
    different string arrays?

    Edit: Are you by chance counting the number of String arrays which contain a specific color?

    Regards,
    Jim
    Last edited by jim829; 10-17-2014 at 04:53 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Comparing String To Array Element ?

    It's better to decompose the problem, i.e. find an element e in an array (or find and count all elements in an array that match an element e), and build what you want from there ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Comparing 2D string array with 1D string array
    By jumpgirl in forum New To Java
    Replies: 1
    Last Post: 03-31-2012, 09:41 PM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Replies: 4
    Last Post: 07-27-2010, 08:52 PM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •