i am new to java. i am trying to use java regular expression to compare two string arrays containing names and then use a nested for loop to print out the names that have a match and names that don't have a match. below is my code
public class Main {
public static void main(String[] args) {
// write your code here
String regex;
Pattern pattern;
String stringToBeMatched;
Matcher matcher;
boolean b;
String [] names1 = {"Peter","Harry","Potter","Mary","Jerry"};
String [] names2 = {"Adam","Jerry","Potter","Martin","Chris", "Rose"};
//try a loop with two variables for loop
for (int x=0;x >= names2.length;x++) {
regex = names2[x];
pattern = Pattern.compile(regex);
for(int y = 0; y >= names1.length; y++){
stringToBeMatched = names1[y];
matcher = pattern.matcher(stringToBeMatched);
b = matcher.find();
if (b) {System.out.println(names1[y] +" has a match");}
else{System.out.println(names1[y] +" has no match");}
}
}
}
}
the code executes and returns the output Process finished with exit code 0
without displaying any message specified by any of the System.out.println()
statements. please guys, what am i not doing right?
Unless I am misunderstanding your intentions, you should not use Regex here. My understanding is you want to compare two arrays for duplicate elements.
You have your arrays:
String[] names1 = {"Peter", "Harry", "Potter", "Mary", "Jerry"};
String[] names2 = {"Adam", "Jerry", "Potter", "Martin", "Chris", "Rose"};
Method 1 - nested for loops.
for(int i = 0; i < names1.length; i++) {
for(int j = 0; j < names2.length; j++) {
if(names1[i].equals(names2[j])) System.out.println(names1[i] + " is in both arrays.");
}
}
Method 2 - nested foreach loops.
for(String name1 : names1) {
for(String name2 : names2) {
if(name1.equals(name2)) System.out.println(name1 + " is in both arrays.");
}
}
Method 3 - using a List .
List<String> names2List = Arrays.asList(names2);
for(String name1 : names1) {
if(names2List.contains(name1)) System.out.println(name1 + " is in both arrays.");
}
Method 4 - using a Set . This works because HashSet#add() will NOT add duplicates, and returns false if requested.
Set<String> names2Set = new HashSet<>(Arrays.asList(names2));
for(String name1 : names1) {
if(!names2Set.add(name1)) System.out.println(name1 + " is in both arrays.");
}
Method 5 - using Streams . If you want to get fancy.
for(String name1 : names1) {
if(Arrays.stream(names2).anyMatch(name1::equals)) System.out.println(name1 + " is in both arrays.");
}
With lambda functions use "distinct" to not print duplicate values.
String [] names1 = {"Peter","Harry","Potter","Mary","Jerry"};
String [] names2 = {"Adam","Jerry","Potter","Martin","Chris", "Rose", "Peter", "Peter"};
for (String name1:
names1) {
Arrays.stream(names2).filter(name1::equals).distinct().forEach(System.out::println);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.