Reputation: 215
I'm writing a simple code using contains()
method, but the problem is that if I put a string array in the argument of contains()
method, the program does nothing (no error). So, it just has to loop over one string array and look whether it contains an element from the second array. I tried with one single word in the argument and it works fine. The arrays are not null and I can print them. So, any suggestion?
This is the part of the code:
for(int i = 0; i < farbe.length; i++) {
for(int j = 1; j < names.length; j++) {
if(names[j].contains(farbe[i])) {
System.out.println(names[j]+"\t"+"yes");
}
}
}
The arrays elements are read from the .*txt file. So, the 'names' array has word sequences and the 'color' array contains the color. The parts from the arrays:
Names:
Herrenlederjacke >>Collins
Herrenstoffjacke >>M H7 4B
9-LV (schwarz | 54)
9-LV (schwarz | 52)
Tunika-Lederkleid >>9519
Color:
beige
braun
schwarz
The arrays are big, so I put just a little part.
Upvotes: 0
Views: 234
Reputation: 124275
Assuming that each line from your file is separate element in array we will have
String[] names={
"Names:",
"Herrenlederjacke >>Collins",
"Herrenstoffjacke >>M H7 4B",
"9-LV (schwarz | 54)",
"9-LV (schwarz | 52)",
"Tunika-Lederkleid >>9519"
};
String[] farbe={
"beige",
"braun",
"schwarz",
};
to check which color exists in names
you can use this code
for (String color : farbe) {//will iterate over all colors
namesLoop:
for(String name :names){//will iterate over all names
if (name.contains(color)){
System.out.println(color+"\t yes");
break namesLoop;//we found this color, we can skip to next one
}
}
}
Upvotes: 0
Reputation: 19103
Your problem may be that java arrays start at index 0. Also, your braces are misplaced in the contains line. Try :
for(int i = 0; i < farbe.length; i++){
for(int j = 0; j < names.length; j++){
if(Arrays.asList(names[j].contains(farbe[i]))){
System.out.println(names[j]+"\t"+"yes");
}
}
Upvotes: 1