Results 1 to 4 of 4
  1. #1
    glockner is offline Member
    Join Date
    Jan 2015
    Posts
    31
    Rep Power
    0

    Default String including parameter

    I am working on a problem that has caused me much grief. I am taking a parameter of type String and checking if either the city or state contains the "keyword", whatever it may be. I am using an arrayList of 10,000 customers and adding any customer that lives in a city or state that contains the keyword to a new arrayList and then returning that arrayList. I thought my solution was correct, but when using the JUnitTest provided, it is not quite passing the test. My solution is as follows.

    Java Code:
        public ArrayList <Customer> getMailingList(String keyword){
            ArrayList <Customer> key = new ArrayList<Customer>();
            keyword = keyword.toLowerCase();
            for(Customer c : data){
                if(c.getState().contains(keyword) || c.getCity().contains(keyword)){
                    key.add(c);
                }
            }
            return key;
        }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: String including parameter

    In what way does it fail? That is, what customer is included in the returned list when they shouldn't be, or not included when they should be?

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: String including parameter

    Do a simple System.out and see what the problem is then. Don't stare at code and guess, make it visible.

    Java Code:
    for(Customer c : data){
            System.out.println("Keyword:" + keyword + "|-|State:" + c.getState() + "|-|City:" + c.getCity());
            if(c.getState().contains(keyword) || c.getCity().contains(keyword)){
                key.add(c);
            }
        }
    I'm sure you'll find it is something silly like the keyword having a space at the start or end of it (which is why I made sure there are no unnecessary spaces in the System.out statement).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: String including parameter

    gimbal2++. That code will reveal space and case problems.

Similar Threads

  1. Replies: 5
    Last Post: 10-07-2013, 01:45 AM
  2. Replies: 7
    Last Post: 04-28-2012, 02:10 AM
  3. Replies: 2
    Last Post: 10-15-2010, 05:09 AM
  4. Replies: 1
    Last Post: 08-22-2008, 06:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •