12

Here is the contents of my ArrayList contain

HPDH-1,001, Check-out date: 7/7/7

JTI-1,001, Check-out date: 7/7/7

My code:

for (int i = 0; i < contain.size(); i++) {
    if (contain.get(i).contains(code)) {
        System.out.println(contain.get(i));
    }
}

The thing is my variable code was String "JTI-1" Why isn't it giving me the JTI-1 output? I am trying to get it to display the value of the variable code. I want to remove the contain.get(i) if it would just give me the one I typed in.

4
  • 1
    "Why isn't it only giving me the JTI-1 output?" What's the question here? What is it displaying that you're not expecting? Commented Oct 19, 2012 at 8:48
  • please include the output of the code, also make sure you have specified the generic type of the ArrayList as <String> Commented Oct 19, 2012 at 8:49
  • 3
    try code.trim() before using it in contains method. Commented Oct 19, 2012 at 8:52
  • Fair play for AmitD's magic ability to guess you had a white space in the string you put in your question... Your code string was apparently not "JTI-1" as you said, since trim() solved your problem. Commented May 22, 2014 at 15:04

4 Answers 4

14

I think fundamentally the code is correct. I would check your inputs and make sure they're really what you think.

I would perhaps rewrite your loop as:

for (String s : contain) {
   if (s.contains(code)) {
      // found it
   }
}

to make use of the object iterators (the above assumes you have an ArrayList<String>). And perhaps rename contain. It's not very clear what this is.

Sign up to request clarification or add additional context in comments.

Comments

11

The code is correct assuming List of strings. I have not modified any of your source code just to give you idea that it works fine.

    List<String> contain = new ArrayList<String>();
    contain.add("HPDH-1,001, Check-out date: 7/7/7");
    contain.add("JTI-1,001, Check-out date: 7/7/7");
    String code = "JTI-1 ";
    for (int i = 0; i < contain.size(); i++) {
        if (contain.get(i).contains(code.trim())) {<---Use trim it is possible that code may have extra space
            System.out.println(contain.get(i));
        }
    }

2 Comments

do i call 'contain.size()' in every iteration again like this?
Arraylist size() function just returns the integer value so it wont make any impact. In above answer as I had mentioned I wasn't looking to refactor the code.
3

Your code ist correct, though I would also advise you to make use of iterators:

import java.util.ArrayList;
import java.util.Arrays;


public class Main {

    public static void main(String[] args) throws Exception {

        ArrayList<String> contain = new ArrayList<String>(Arrays.asList("HPDH-1,001, Check-out date: 7/7/7",
                "JTI-1,001, Check-out date: 7/7/7"));

        String code = "JTI";

        // your loop
        for (int i = 0; i < contain.size(); i++) {
            if (contain.get(i).contains(code)) {
                System.out.println(contain.get(i));
            }
        }

        // my suggestion
        for (String s : contain) {
               if (s.contains(code)) {
                  System.out.println(s);
               }
        }
    }
}

Output:

JTI-1,001, Check-out date: 7/7/7

JTI-1,001, Check-out date: 7/7/7

If this output is not what you want, please add more information.

Comments

0
System.out.println("Find name");
        String str = s.nextLine();
        List<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < surname.size(); i++) {
            if(surname.get(i).equals(str)){
                result.add(i);
            }
        }
        for (Integer integer : result) {
            System.out.println(integer);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.