Add values to a HashSet by checking the input of the method. for example: if inp
ID: 3727024 • Letter: A
Question
Add values to a HashSet by checking the input of the method.
for example: if input AAA111, and the key of enr AAA323: ( if letters matches)
add the value of both to the Set
this solution is wrong:
public static Set match(String a) i 89e 90 91 92 93 94 95 96 97 98 unit HashSet0; new Set = Pattern p = Pattern. compile(" [A-Za-z]+"); Matcher m = p.matcher(a); entry enn.entrySet()) { : for (Map. Entry for (String value: entry.getValueO) I 100 101 102 103 104 105 106 107 108 109 110 if (entry.getKeyC) == m.groupC)) { unit.add(value); return unitExplanation / Answer
public static Set<String> match(String a) {
Set<String> unit = new HashSet<String>();
Pattern p = Pattern.compile("(^[A-Za-z]+)(\d+)");
Matcher matcher = p.matcher(a);
// to find if input string matches the pattern
if(matcher.find()) {
// Split only Letters from input string
String aMatch = matcher.group(1);
for (Map.Entry<String, Set<String>> entry : enr.entrySet()) {
for (String value : entry.getValue()) {
matcher = p.matcher(value);
// to find if enr value matches pattern
if (matcher.find()) {
// Split only Letters from enr value string
String bMatch = matcher.group(1);
// to check if both the letters are equal. If case of letters can be ignored then use equalsIgnoreCase in the place of equals
if(aMatch.equals(bMatch))
unit.add(value);
}
}
}
}
}
Try this code and share the feadback in comment section. Rate it
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.