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.
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.
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));
}
}
For-each loop ArrayList
How to fill out an ArrayList with for each loop? (Java) - Stack Overflow
Is it possible to iterate over a std.array_list with a for loop ?
Beginner Java Question: ArrayList - foreach into for loop with i-variable
Why do we use forEach in Java?
What is the primary difference between forEach and for loop?
Can forEach transform elements in an array?
Videos
I am in the third part of a MOOC course on Java. And I'm a little confused about the ambiguity of using the For-each loop to enumerate a list.
Consider the following list as an example
ArrayList<Integer> list = new ArrayList<>();
The examples and answers present two implementations:
for (Integer val: list) {
//do something
}
for (int val: list) {
//do something
}
But it is not explained how one differs from the other and when one or the other variant should be used. Please tell me the difference between the two.
The solution is simple
final int[] numbers = new int[] {2,4,6,8,10,12,14,16,18,20};
final List<Integer> list = new ArrayList<Integer>();
for (int number : numbers) {
list.add(number);
}
Something like this maybe?
IntStream.range(0, 10).map(y -> 2 + y * 2).forEach(arraylist::add);